我是python的初学者,但我对C ++有所了解,我的问题是我试图获取用户给出的所有值的总和,但我收到此错误“ int”对象不是可迭代的,所以有人请帮助我,这是我的代码
Food= int(input("Enter number of Food: "))
for x in range(Food):
Foodn = str(input("Enter Food Name: "))
Value = int(input("Enter Value: ))
上面的代码有效
#--Getting the Sum of all Value
for j in Value:
j += Value
print(j)
第一:
str(input())
变成
input() #By default its a string but it doesn't really matter
然后:如果您在for循环中使用j,则由于更改了它的值而无法正常工作,您需要做的是这样的事情
Food= int(input("Enter number of Food: "))
List = []
for x in range(Food):
Foodn = str(input("Enter Food Name: "))
Value = int(input("Enter Value: "))
List.append(Value)
Total = 0
for j in List:
Total += j
print(Total)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句