TypeError:' int'对象在while循环中不可迭代

时间:2018-02-18 02:37:48

标签: python python-3.x int

我需要一些快速帮助, 我一直有这个问题: " TypeError:' int'对象不可迭代"在第8行 在这段代码中:

sold = 0
max = 0

for i in range(0,5):
    print ("Scout",(i+1),"> ",end="")
    x = int(input())
    sold = sold + x
   for i in x:
       if i > max:
           max = i
print (max)

任何人都可以帮我或重新安排这个吗?我试图让python从值x中找到最大值。

3 个答案:

答案 0 :(得分:0)

如何将值添加到这样的列表中:

nums = [] 

然后代替

x = int(input())

nums.append(int(input())

然后尝试

for x in nums:
    If x > max:
        max = x

确保你的缩进正确。在python中,缩进确定代码是否运行,即使缩进是一个,它也会产生错误或意外的副作用。

答案 1 :(得分:0)

这就是我重新安排

的方式
v = []  # create a list where you can store all the values
for i in range(5):
    print("Scout {} > ".format(i + 1), end="")
    v.append(int(input()))  # read it and append it to the end of the list
print(max(v))  # your 'max' variable
print(sum(v))  # your 'sold' variable

请注意,命名变量max通常是一个坏主意,因为它会覆盖我刚才使用的内置函数。

答案 2 :(得分:0)

以下是代码的缩短版和工作版:

v = [int(input("Scout {} > ".format(i + 1))) for i in range(5)]
print('max: {}'.format(max(v)))
print('sold: {}'.format(sum(v)))