此代码应打印斐波那契数列的前十个数字中的偶数之和。
#Creates a list with the first ten Fibonacci numbers.
l = [1,2]
for i in range(10):
l.append(l[i]+l[i+1])
for i in l:
#If an element of the Fibonacci list is uneven, replace it with zero.
if l[i]%2 != 0:
l[i] = 0
#Print the sum of the list with all even Fibonacci numbers.
print sum(l)
当我执行此操作时,我得到:
File "pe2m.py", line 6, in <module>
if l[i]%2 != 0:
IndexError: list index out of range
我不明白它是如何超出范围的,有人可以澄清一下吗?