如何修复类型错误:'int'对象不可迭代



我的代码:

def main():
number = None
quit = False
while(not quit):
choice = getSelection()
if(choice == 'G'): # get a number
number = getANumber()
elif(choice == 'S'): # current sum
currSum(number)
elif(choice == 'A'): # current avg
currAvg(number)
elif(choice == 'H'): # highest number
currHighest(number)
elif(choice == 'L'): # lowest number
currLowest(number)
elif(choice == 'Q'): # quit    
print('Thanks for using the program!')
quit = True
pause()
else:
print('Invalid selection. Pick only from the menu.')
pause()

def displayMenu():
print("n"*50, "=== The Menu ===")
print("G] Get a number")
print("S] Display current sum")
print("A] Display current average")
print("H] Display the current highest number")
print("L] Display the current lowest number")
print("Q] Quit")
print("n" * 2)
def getSelection():
displayMenu()
result = input("Enter your selection: ")
return result
def getANumber():
result = int(input("Enter a number, please: "))
return result

def currSum(num):
if(num is None):
print("Go back and enter a number first...")
pause()
return 
print("The current sum is: ", sum(num))
pause()
def currAvg(num):
if(num is None):
print("Go back and enter a number first...")
pause()
return
avg = sum(num) / len(num)
print("The current avgerage is: ", round(avg,2) )
pause()
def currHighest(num):
if(num is None):
print("Go back and enter a number first...")
pause()
return
print("The current highest is: ", max(num))
pause()
def currLowest(num):
if(num is None):
print("Go back and enter a number first...")
pause()
return

print("The current lowest is: ", min(num)
pause()
def pause():
x = input("npress any key to continue....n")

main()

错误:

Traceback (most recent call last):
File "d:pythonProjectweek11menuFuncc.py", line 85, in <module>
main()
File "d:pythonProjectweek11menuFuncc.py", line 10, in main
currSum(number)
File "d:pythonProjectweek11menuFuncc.py", line 51, in currSum
print("The current sum is: ", sum(num))
TypeError: 'int' object is not iterable

我有这个学校的编码项目,我必须存储从用户输入然后找到的总和,平均值,最大值和最小值,使用这个菜单类型的程序,最好是教授想要它不使用列表,但我似乎不能没有一个工作。我不知道如何通过这个错误,我已经读了一些关于这个' TypeError '的其他帖子,但不知何故无法通过它。

问题是,你正在使用的函数,如sum(), avg()等不工作在单个整数。

sum()函数返回一个数字,即可迭代对象中所有项的和。

要做到这一点,你必须去一个小手册。

此外,知道你的教授已经限制你使用,列表这意味着(在我看来)他/她也希望你手动计算所需的东西,以提高你的编程技能。

我已经更正了你的代码如下:

def main():
global cur_sum
global no_of_digits
global highest_num
global lowest_num
cur_sum = 0
no_of_digits = 0
number = None
quit = False
while (not quit):
choice = getSelection()
if (choice == 'G'):  # get a number
number = getANumber()
no_of_digits += 1
cur_sum += number
if no_of_digits == 1:
highest_num = number
lowest_num = number
prev_number = number
if number > prev_number:
highest_num = number
elif number < prev_number:
lowest_num = number
prev_number = number
elif (choice == 'S'):  # current sum
currSum(number)
elif(choice == 'A'): # current avg
currAvg(number)
elif(choice == 'H'): # highest number
currHighest(number)
elif(choice == 'L'): # lowest number
currLowest(number)
elif(choice == 'Q'): # quit
print('Thanks for using the program!')
quit = True
pause()
else:
print('Invalid selection. Pick only from the menu.')
pause()

def displayMenu():
print("n" * 50, "=== The Menu ===")
print("G] Get a number")
print("S] Display current sum")
print("A] Display current average")
print("H] Display the current highest number")
print("L] Display the current lowest number")
print("Q] Quit")
print("n" * 2)

def getSelection():
displayMenu()
result = input("Enter your selection: ")
return result

def getANumber():
result = int(input("Enter a number, please: "))
return result

def currSum(num):
if (num is None):
print("Go back and enter a number first...")
pause()
else:
print("The current sum is: ", cur_sum)
pause()
def currAvg(num):
if (num is None):
print("Go back and enter a number first...")
pause()
return
avg = cur_sum/no_of_digits
print("The current avgerage is: ", round(avg, 2))
pause()

def currHighest(num):
if (num is None):
print("Go back and enter a number first...")
pause()
print("The current highest is: ", highest_num)
pause()

def currLowest(num):
if (num is None):
print("Go back and enter a number first...")
pause()
print("The current lowest is: ", lowest_num)
pause()

def pause():
x = input("npress any key to continue....n")

main()

我做的两件事和你不一样:

  1. 在代码之前将变量声明为全局变量,以便我可以在程序的任何地方访问它们。

  2. 手动计算总和、平均值、最高值和最低值。

除了我上面提到的小错误之外,你的代码总体上是好的:)

相关内容

  • 没有找到相关文章

最新更新