Reputation: 1
How to output results in list form (with code not manually) on Python 2.75.
Instead of solving just for the sum I inserted print i
then sum and got the numbers visible (11 of them). Please help with this very basic question. Thank you.
##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]
Upvotes: 0
Views: 435
Reputation: 11691
Start with an empty list and as you find them append them on:
even_fib = []
for i in a:
if i %2 == 0:
even_fib.append(i)
The result will be a list of all the even elements
Upvotes: 1