Two equal programs providing different outputs

Asked

Viewed 150 times

5

I’m developing a program that’s one of those classic programming exercises that involves the Fibonacci sequence. The program is simple, it asks the user to enter a term of the sequence and the program tells which number of the sequence occupies that term. Example: The 5th term of the sequence is 5, the 10th term is 55 and so on.

I have developed the following code for this:

n = int(input("Digite o termo da sequência Fibonacci: "))
a = 1
b = 1
k = 1
while k <= n - 2:
    a = b
    b = a + b
    k = k + 1
print("O {}º da Sequência de Fibonacci é ocupado pelo número {}.".format(n,b))

However, when I run the program, it does not work as expected. See:

Digite o termo da sequência Fibonacci: 5
O 5º da Sequência de Fibonacci é ocupado pelo número 8.

Again:

Digite o termo da sequência Fibonacci: 10
O 10º da Sequência de Fibonacci é ocupado pelo número 256.

I looked for solutions on the internet to see where I was going wrong and found in the Python course for Zombies a solution that solves the problem correctly:

n = int(input("Digite o termo da sequência Fibonacci: "))
a, b = 1, 1
k = 1
while k <= n - 2:
    a, b = b, a + b
    k = k + 1
print("O {}º da Sequência de Fibonacci é ocupado pelo número {}.".format(n,b))

Watch the exits:

Digite o termo da sequência Fibonacci: 5
O 5º da Sequência de Fibonacci é ocupado pelo número 5.

Again:

Digite o termo da sequência Fibonacci: 10
O 10º da Sequência de Fibonacci é ocupado pelo número 55.

As you can see, the last two exits are correct.

The big problem is that, at least in my view, the codes are exactly the same, the difference is that one occupies fewer lines than the other, or am I wrong? Why the exits are different?

  • The order in which things happen...;a, b = b, a + b is equivalent to b = a+b and then a = b

3 answers

8


Because the codes aren’t the same, they’re very different.

In your code you’re adding a with b, only just before you said that a is the same value as b, so in practice you’re always adding up b, with b. For this to work you would have to change the value of a after making the sum, but for this to work would have to create a temporary variable, you cannot change the value of b before making change. See:

a, b = b, a + b

At the same time you’re saying that a become equal to b, and b is equal a + b and the value of a has not yet been modified. It allows a swap without temporary storage.

In the code you picked up it does an assignment like tuples, meaning it does operations concomitantly, so when you do a + b the value of a is the original value, the change in value of a for b it hasn’t happened yet, so everything works out fine.

This Python feature is a beauty because it causes this, it does not need a temporary variable, and it is the idiomatic form of the language, prefer to do so.

Your code would work that way:

n = int(input("Digite o termo da sequência Fibonacci: "))
a = 1
b = 1
k = 1
while k <= n - 2:
    tmp = a
    a = b
    b = tmp + b
    k = k + 1
print("O {}º da Sequência de Fibonacci é ocupado pelo número {}.".format(n,b))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • So when I write in the same line or in different lines it makes a difference?

  • 2

    Yes, language is a sequence of commands, their order makes a difference, unless it is the same. You don’t have to just change the line, just change the order on the same line. If you change the order of a sum or multiplication it changes nothing, but if you change the order in a subtraction or division it completely changes the result. In this case putting in separate lines does what I explained.

4

The programs are not the same.

When you did

a = b

you just lost the original value of a.

Your program should circumvent this problem as follows,

temp = a
a = b
b = temp + b
k = k + 1
  • 1

    I don’t understand why he should circumvent the problem with a temporary variable if he has already bypassed the idiomatic form of deconstructing tuples: a, b = b, a+b.

4

The problem is that expressions...

a, b = b, a + b

and

a = b
b = a + b

...are not equal and do not have the same result.

In the first expression the value of a on the right side of the expression (after the =) has not yet undergone the desired change. So the operation a, b = b, a + b amounts to:

t = a # o valor de 'a' é armazenado em uma variável temporária.
a = b 
b = t + b # aqui 'b' recebe 'b' mais o antigo valor de 'a'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.