Python Forum
Assistance with Fibonacci code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Assistance with Fibonacci code (/thread-4342.html)



Assistance with Fibonacci code - Colombian1976 - Aug-09-2017

hello There,

There is an "logic" error on my code, and I have not figured it out, what it is!!

Code:

#Fibonacci numbers

x = 1
y = 1
z = 0

while z < 1000:
x = y
y = z
z = x + y
print(z)
Why is it printing above 1000? What am I missing here? (Note, I am still very new to Python)

any help appreciated

Cheers
Colombian1976


RE: Assistance with Fibonacci code - ichabod801 - Aug-09-2017

Assuming your code is indented like this:

x = 1
y = 1
z = 0
 
while z < 1000:
    x = y
    y = z
    z = x + y
    print(z)
It prints one number over 1000 (1597). That's because z < 1000 is only checked at the while statement. It goes through all of the indented code, goes back to the while statement, and then checks the condition. So on the last iteration of the loop, it adds 610 and 987 and puts that in z, prints z, and then sees that z is over 1000 and stops the loop.

The easiest way to fix this is to put print(z) at the top of the loop, but you have to swap the initial values of y and z for that to work.

Have you done tuple assignment yet? That's very helpful for Fibonacci numbers.

x, y = y, x+y



RE: Assistance with Fibonacci code - Karthikeyan - Feb-16-2024

You have missed the increment of control variable 'z' in your program. That is why it printing above 1000 elements. In Fact the program prints the elements continuously until you stop the running.

"while" loop executes a block of code associated with it till the given condition becomes false. In your program the condition is 0<1000 which means the block of executed until 0 is less than 1000. So you should add the increment statement in the code-block.
like

while z < 1000:
x = y
y = z
z = x + y
print(z)
z=z+1

In this corrected program z incremented each time and the program will stop printing when z=1001.

For further details visit my webpage
https://wallbreakerhero.blogspot.com/2024/02/python-program-for-fibonacci-series.html


RE: Assistance with Fibonacci code - Pedroski55 - Feb-17-2024

11 is a very strange number.

Fibonacci is a very strange sequence.

For every Fibonacci sequence of length 10, the sum of the sequence is equal to the 7th term times 11!

As long as you enter 10 in myApp() the sum of the sequence is equal to the 7th term times 11!

def myApp(num):
    if num < 7:
        print("Please enter a number >= 7.")
        return
    one = random.randint(1, 100)
    two = random.randint(1, 100)
    print(f'first number is {one}, the second number is {two}.')
    myfibo = [one, two]
    for n in range(2, num):
        nu = myfibo[n-2] + myfibo[n-1]
        myfibo.append(nu)
    for i in myfibo:
        print(i)
    print(f'the length of myfibo = {len(myfibo)}')
    print(f'The sum of myfibo is {sum(myfibo)}')
    print(f'{myfibo[6]} x 11 = {myfibo[6] * 11}')
    return myfibo