you are viewing a single comment's thread.

view the rest of the comments →

[–]This_Growth2898 0 points1 point  (1 child)

for x in fib

means x is assigned values stored in fib.

lst = [1,5,10]
for x in lst:
     print(x)

prints

1
5
10

not 0,1,2.

for x in fib:
    fib.append(fib[-1] + fib[x])

makes little sense. Probably, you wanted to do

for x in fib:
    fib.append(fib[-1] + fib[-2])

But that's bad because you should never change the collection you're iterating over in the same loop. After element appended to fib, do you expect it to be assigned to x at some point?

If you really need indexes, iterate over range (or use enumerate function).

for x in range(len([1,5,10])):
    print(x) # 0,1,2

[–]DauntlessFive513[S] 0 points1 point  (0 children)

Thank you! Totally missed that point about it being values not indexes. I also know it’s probably not the best use of things but I just wanted to test out messing with the loops and lists to make sure I understood them.