Jun-10-2020, 01:58 PM
Hi, I am new to programming and am self-learning, using projecteuler as a source of questions. I am trying to create a program which generates the fibonacci sequence until the nth term is sufficiently large, and put all the values in a list. My program is as follows:
In this example, I want it to print out every Fibonacci number less than 100. However, it always prints two more values after my upper bound: in this case, it adds 89 to the list (which I want), followed by 144 and 233. I am wondering if I have a conceptual misunderstanding of how "for _ in _" statement works, as I would've thought it works as follows:
adds first couple of terms fine, then gets to 89, recognises that 89 is less than 100 so adds the next term to the list (144), checks to see if 144 is less than 100, which it isn't, and so does not add 233 to the list.
Apologies if this is a simple mistake, as I said I am new to python.
Thanks.
x = [0, 1] for j in x: if j > 100: break else: x.append(x[-1]+x[-2]) print(x)
In this example, I want it to print out every Fibonacci number less than 100. However, it always prints two more values after my upper bound: in this case, it adds 89 to the list (which I want), followed by 144 and 233. I am wondering if I have a conceptual misunderstanding of how "for _ in _" statement works, as I would've thought it works as follows:
adds first couple of terms fine, then gets to 89, recognises that 89 is less than 100 so adds the next term to the list (144), checks to see if 144 is less than 100, which it isn't, and so does not add 233 to the list.
Apologies if this is a simple mistake, as I said I am new to python.
Thanks.