我运行这段代码是为了找到偶数之和。 这是我的密码
from functools import reduce
liste = [1,2,3,4,5,6,7,8,9,10]
def cift_mi(a):
if (a % 2 == 0):
return True
return False
ciftler = (list(filter(cift_mi,liste)))
def toplama(a,b):
return a + b
sonuc=(list(reduce(toplama,list(ciftler))))
print(sonuc)
运行代码时,我收到以下错误:
TypeError: 'int' object is not iterable
要了解这一点,请查看错误发生在哪一行:
它显示了发生错误的行,但该行发生了几件事,因此我们可以将其分解为单独的部分:
现在错误发生了变化:
错误是说
list
正在尝试迭代一个值,但该值是一个int,因此它不能。这意味着reduced_value
必须是一个int。因此,reduce
函数必须返回一个int这是有意义的,因为reduce函数
toplama
返回a + b
,所以reduce
将返回ciftler
中所有值的总和。它的返回值是一个int,而不是一个list,因此不能对它调用list()
您可能希望任务如下所示:
相关问题 更多 >
编程相关推荐