I am learning python in college, and have been having trouble with some concepts, mainly loops. I need help understanding why my code is producing an incorrect result:
def fibonacci(maxint):
a, b = 0, 1
LIST = [a, b]
while b < maxint:
a, b = b, a + b
LIST.append(b)
return LIST
In order to test the script, I need to call the function and pass 10 as an argument in the console:
>>>fibonacci(10)
The output should show as:
[0, 1, 1, 2, 3, 5, 8]
but it is actually printing:
[0, 1, 1, 2, 3, 5, 8, 13]
Can someone please analyze my code and tell me the following:
- where is it inefficient?
- Why is it going out of bounds and looping past 10 (the argument from my function)?
b < maxint, then updateb(which can go out of bounds) and finally append the updated one...fibonacci(0). In that case I would expect an empty list?maxint