- Nov 18, 2012
- 2,375
- 0
- 76
I'm trying to build a Fibonacci series in Python 3.X for an assignment. I'm supposed to use lists and loops to get it to happen, and this is what I'm trying:
Anyone have some advice?
When I run it through the module, it gives me an error (emphasis added):N = int (input ("Enter an integer: "))
fib = [0]*(N)
fib[0] = 1
fib[1] = 1
indexNum = 2
while N > 0:
fib[indexNum] = fib[indexNum - 1] + fib[indexNum - 2]
indexNum = int(indexNum + 1)
N = N - 1
print (fib)
But if I simply type it out line by line in the module (without the loop part-- I enter it in manually) it works fine.Traceback (most recent call last):
File "C:\Users\Kyle\Documents\Homework\Python Class 2013\Week 3\Fibonacci.py", line 7, in <module>
fib[indexNum] = fib[indexNum - 1] + fib[indexNum - 2]
IndexError: list assignment index out of range
Anyone have some advice?
