为什么我得到 'int' 类型的参数不可迭代?
why am I getting argument of type 'int' is not iterable?
Python 这里是编码新手。我知道可能还会弹出此错误的其他实例,并且我已尝试在堆栈溢出时阅读它们,但以我的经验水平解码它似乎太技术化了。有人可以帮助解释为什么这不起作用吗?我正在尝试使用 while 循环来要求用户输入数字。打印用户输入的所有数字的总和 运行,如果用户输入 0,则退出 while 循环。
total = []
i = 1
total = int(input('enter a number:'))
while i in total > 0:
total = int(input('enter a number:'))
total = total + i
if i == 0:
break
while i in total > 0:
使用运算符链接规则进行解析,这使其等同于:
while i in total and total > 0:
问题是total > 0
测试; total
是一个 list
,它正在尝试进行字典序比较。你的另一个问题是:
total = total + i
list
无法添加 int
。
固定代码如下所示:
total = 0 # total should be an int, not a list to accumulate the sum
while True: # The if check at the end makes it pointless to test or initialize i up front
i = int(input('enter a number:'))
total += i
if i == 0:
break
Python 这里是编码新手。我知道可能还会弹出此错误的其他实例,并且我已尝试在堆栈溢出时阅读它们,但以我的经验水平解码它似乎太技术化了。有人可以帮助解释为什么这不起作用吗?我正在尝试使用 while 循环来要求用户输入数字。打印用户输入的所有数字的总和 运行,如果用户输入 0,则退出 while 循环。
total = []
i = 1
total = int(input('enter a number:'))
while i in total > 0:
total = int(input('enter a number:'))
total = total + i
if i == 0:
break
while i in total > 0:
使用运算符链接规则进行解析,这使其等同于:
while i in total and total > 0:
问题是total > 0
测试; total
是一个 list
,它正在尝试进行字典序比较。你的另一个问题是:
total = total + i
list
无法添加 int
。
固定代码如下所示:
total = 0 # total should be an int, not a list to accumulate the sum
while True: # The if check at the end makes it pointless to test or initialize i up front
i = int(input('enter a number:'))
total += i
if i == 0:
break