Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm new to python. These two executions of the while loop for producing a Fib arrangement are bringing about totally different outputs. The first is returning the force series of 2, however, I feel it is ought to do precisely what the last is; which is returning the normal series. The second while loop is clearly accomplishing something right. I'm getting it has to do with the way the factors as being appointed while swaping the values.

I just want to know, which was driving these differences?

First while loop:

def fib(n):

x=0 

y=1

while y < n:

    print(y)

    x = y

    y = x + y

The second while loop:

x,y=0,1

while y < 100:

    print(y)

    x,y = y,x+y

closed

4 Answers

0 votes
by (25.7k points)
 
Best answer
The differences in output between the two while loops are due to the different assignments of values to variables x and y within each loop.

Let's examine each loop and its behavior:

First while loop:

def fib(n):

    x = 0

    y = 1

    while y < n:

        print(y)

        x = y

        y = x + y

In this loop, the initial values of x and y are set to 0 and 1, respectively. Inside the loop, the value of y is printed, then the values of x and y are updated. However, the line y = x + y assigns the sum of x and y to y, which leads to incorrect Fibonacci sequence generation. Instead, it should assign the previous value of y to x and the sum of the previous values of x and y to y.

Second while loop:

x, y = 0, 1

while y < 100:

    print(y)

    x, y = y, x + y

In this loop, the values of x and y are simultaneously updated in a single line using a technique called tuple unpacking. Here, the value of x is assigned the current value of y, and the value of y is assigned the sum of the previous values of x and y. This ensures that the Fibonacci sequence is generated correctly.

To fix the first while loop and make it produce the Fibonacci sequence correctly, you can modify the assignment of x and y as follows:

def fib(n):

    x = 0

    y = 1

    while y < n:

        print(y)

        x, y = y, x + y

With this modification, both while loops will generate the Fibonacci sequence as expected.
0 votes
by (26.4k points)

No problem. Following the succession of your first loop,  so x=0, y=1. The principal(first) line in your loop makes x = y = 1

So now y=1

At that point, your next line makes y=x+y. Which implies y=1+1=2. Presently, x=1 and y=2.

For the 1st iteration:

x=y=2

so, x=2

then:

y=2+2=4

As was clarified, your subsequent loop is an equal task. So following the rationale beginning with x=0 and y=1: 

x,y=y,x+y

causes:

x=1 and y= 0+1 = 1 simultaneously

So, right now x=1 and y=1. Then for the next iteration:

x=1 and y= 1+1=2 simultaneously

So now x=1 and y=2. My educator for my software engineering class instructed me that after code line by line on paper comprehends the loop the PC follows. I likewise discovered it was acceptable to help construct a function to understand code

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

0 votes
by (15.4k points)
The differences in output between the two while loops are due to the assignment of values to variables x and y. In the first loop, the assignment y = x + y is incorrect for generating the Fibonacci sequence. In the second loop, the correct assignment x, y = y, x + y is used to update the variables properly.
0 votes
by (19k points)
The variations in output between the two while loops stem from how the values are assigned to variables x and y. In the first loop, the assignment y = x + y is incorrect for generating the Fibonacci sequence. On the other hand, the second loop correctly assigns values using x, y = y, x + y, resulting in the desired Fibonacci sequence.

Related questions

0 votes
2 answers
0 votes
1 answer
asked Mar 9, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Oct 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)

Browse Categories

...