我有一个以TypeError结尾的函数,我不确定为什么:#under 4 milliondef fib(a, b): a, b = 0, 1 while b <= 4000000: a, b = b, (a+b) print a#always call it with "fib(0, 1)"fiblist = [fib(0, 1)]print fiblist#now for the function that finds the sum of all even fib'stotal = 0for i in fiblist: if i%2 == 0: total = total + i print total这是错误消息:Traceback (most recent call last): File "C:\Python27\ProjectEuler\ProjectEuler2.py", line 19, in <module> if i%2 == 0:TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'>>>
2 回答
慕仙森
TA贡献1827条经验 获得超7个赞
修复fib函数,使其返回某些内容。另外,请注意,您不必向其传递任何参数:
def fib():
a, b = 0, 1
lst = []
while b <= 4000000:
a, b = b, (a+b)
lst.append(a)
return lst
还要修复此行:
fiblist = fib()
现在fiblist将包含实际数字,您可以安全地对其进行迭代。这样可以解决您遇到的错误!
添加回答
举报
0/150
提交
取消