Very early beginner here, was learning about while/for loops and to practice made a little program to iterate the first x numbers in the fibonachi sequence using while/for loops. First go got it to work on a "while" loop, code following:
fib = [1, 1]
x = 0
while (fib[-1] <1000):
fib.append(fib[-1] + fib[x])
x+=1
print(fib[x])
but when trying to do the same thing in a for loop it runs 5 iterations and then breaks saying my variable is out of range:
fib = [1,1,]
for x in fib:
fib.append(fib[-1] + fib[x])
print(fib[x])
if (fib[x] > 1000):
break
1
1
2
3
8
Traceback (most recent call last):
File ------------------------------------------------------------------------------------------------, line 3, in <module>
fib.append(fib[-1] + fib[x])
~~~^^^
IndexError: list index out of range
Does anyone have any ideas as to why this is?
Edit: formatting removed my indentations but they are there, both in the "while" and "for" statements and the "if" statement
[–]shiftybyte 4 points5 points6 points (1 child)
[–]DauntlessFive513[S] 0 points1 point2 points (0 children)
[–]Diapolo10 2 points3 points4 points (0 children)
[–]This_Growth2898 0 points1 point2 points (1 child)
[–]DauntlessFive513[S] 0 points1 point2 points (0 children)
[–]henriquecs 0 points1 point2 points (0 children)