0

如何在 Python 2.75 上以列表形式输出结果(代码不是手动)。而不是只求解我插入的print i总和,然后求和并得到可见的数字(其中 11 个)。请帮助解决这个非常基本的问题。谢谢你。

##Even Fibonacci numbers Problem 2
##Each new term in the Fibonacci sequence is generated by adding the previous two terms.
##By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55...
##By considering the terms in the Fibonacci sequence whose values do not exceed
##four million, find the sum of the even-valued terms.
import fibo
sum = 0
a = fibo.fib2(4000000)
for i in a:
    if i%2==0:
        print i
        sum += i
print "the sum of these even Fibonacci numbers = "
print sum
###how to create list or tuple of the
##even_fib=[2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578]
4

1 回答 1

1

从一个空列表开始,当您发现它们时,将它们附加到:

even_fib = []

for i in a:
    if i %2 == 0:
        even_fib.append(i)

结果将是所有偶数元素的列表

于 2013-06-25T17:19:46.147 回答