0

I have this program which calculates the terms in the Fibonacci sequence; once finished the process i intend to evaluate all even-values and add them to an empty list, then sum all the terms in the list. But the only line i get for the sum process is ""

NOTE: There are tabulators after the "for" line and the "if" line

nt=input("Ingrese el numero de terminos:", )


 w=1                                                                                                
 x=0                      
 y=0            
 z=1          
 print w

for i in xrange(2,nt+1):                                                                              
     y=z+x            
     x=z          
     z=y      
     print z

if z%2==0:             
    list=[]         
    list=list+[input(z)]   
    sum(list)  
print sum
2
  • Is your indentation exactly right ? I think your "if" should be indented after the print. Commented Jul 31, 2014 at 14:13
  • Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, ... or 1, 1, 2, 3, 5, 8, .... Your code produces 1, 2, 3, 5, 8, ... which is not strictly correct. Commented Jul 31, 2014 at 14:32

4 Answers 4

1

A somewhat simpler way:

nt=input("Ingrese el numero de terminos:", )
fib=[0,1]
for i in range(nt-2):
    fib.append(sum(fib[-2:]))
print sum(i for i in fib if i%2 == 0)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to acutally assign that to a variable

total = sum(list) 
print total

Right now, there is no variable for sum to return to, so it will calculate the sum, then just move on.

Comments

0
# Note; both sum and list are built in.  You do not want to have variables
# using these names.
#
# A minimal modification to make it work on both Python2 and Python3.

from __future__ import print_function


nt=int(input("Ingrese el numero de terminos:", ))

w=1
x=0
y=0
z=1
print(w)

lst=[]

for i in range(2,nt+1):
     y=z+x
     x=z
     z=y
     print(z)

     if z%2==0:
        lst.append(z)

print("The sum is", sum(lst))

Comments

0

I think your indentation is wrong too, and your code resets your list everytime you find an even number, 2) you should not use "list" as a variable, as it is a special word in python and can cause issues. 3) you don't need to use input when you add something to a list.

Try this version :

nt=input("Ingrese el numero de terminos:", )

lst =[]
w=1                                                                                          
x=0     
y=0            
z=1     # could replace 4 lines with w,x,y,z = 1,0,0,1     
print w

for i in xrange(2,nt+1):                                                                              
     y=z+x            
     x=z          
     z=y
     print z

     if z%2==0:                      
         lst=lst+[z]   # Would be better to use append   
 total = sum(list)  
 print total

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.