首页 > 解决方案 > 我们可以在 for 循环中声明一个空函数吗?

问题描述

def dec_list():
    a = list()
    for i in range(0, 3):
        i = int(input("Enter the values: "))
        a.append(i)
    print(a)
x = dec_list()
print(x)
output:
[1,2,3]
total = 0
for num in x:
    total = total + num
    print(total)
output:
    for num in x:
TypeError: 'NoneType' object is not iterable

关于代码的第一部分,我的输出正常(即 [1,2,3] ),但是当我尝试使用函数运行 for 循环时,会发生错误。你能帮我解决这个问题吗?

(对不起,如果问我问题的方式不恰当,这是我的第一次)

谢谢!

标签: pythonpython-3.xlistfunctionfor-loop

解决方案


类似的东西?

total = 0
for num in x:
    pass

推荐阅读