Reputation: 65
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
Upvotes: 0
Views: 298
Reputation: 9704
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
Upvotes: 0
Reputation: 87054
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)
Upvotes: 1
Reputation: 1253
# 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))
Upvotes: 0
Reputation: 117856
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.
Upvotes: 0