このコードは、フィボナッチ数列の最初の10個の偶数の合計を出力する必要があります。
#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
範囲外になるのかわかりませんが、誰かが明確にできますか?