Learning python for two days 🙂 and now I attempting to solve Project Euler problem #2 and I need help.
To be more specific, I need know know how to add numbers that were added to a empty list. I tried ‘sum’ but doesn’t seems to work how the tutorial sites suggest. I’m using python 3. Here’s my code so far:
a = 0
b = 1
n = a+b
while (n < 20):
a, b = b, a + b
n = a+b
if n%2 == 0:
mylist = []
mylist.append(n)
print(sum(mylist))
this outputs:
2
8
Now how do I add them? Thanks 🙂
You are doing it right (the summing of the list), the main problem is with this statement:
move it before the
whileloop. Otherwise you are creating an new empymylisteach time through the loop.Also, you probably want to print the sum of the list probably after you are done with your loop.
I.e.,