Same codes, different outputs

Asked

Viewed 145 times

-1

Why in the code

x = 1
while (x < 4):
    x = x + 1
print ("x vale", x)

has only one output 4, and in

i = 6
while (i > 0):
    i = i - 3
    print (i)

The way out is different.

  • 1

    I think the thought remained: "...has only one way out 4, while the following code has two saturdays 0 and 3..."

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

2

The question has a clearly wrong premise, they are not equal codes, they are quite different. Any change in the code, however minimal, may produce different results.

The first value starts with 1 and will have 3 interactions because in each one the value of the variable increases by 1 and the condition establishes that at the moment it arrives at 4 it must close the loop.

In fact the code so does not make the slightest sense because it is sure that the code will always print 4, so it is better to have 4 printed without doing anything else. Make the code as simple as possible.

It may be that he wanted the first code to print 2, 3, 4 on each line. This would only happen if the print() was in the same block as the while, thus:

x = 1
while (x < 4):
    x = x + 1
    print ("x vale", x)

Note that by the order of things do not print the number 1, the increment would have to come after the printing to print the 1, there would not print the 4.

The second code already starts different because the initial value of the variable is 6 and the condition to close the loop is that the value has to be greater than 0. Internally in each step the variable will have its value reduced by 3. Then in the first step the variable will be worth 3, and in the second step it will be worth 0, so there will not be a third step since the condition becomes false.

Another big difference is that the impression of the value is inside the loop (it is indented, has a spacing indicating that it is part of that block and not of another more external one). Then at each step the value is printed. At the end of the loop nothing is printed, there is no command outside it as it is in the first code.

If you wanted only the final value to appear then the print() should be out of the loop. It will always be 0, but in some case the initial value was different the end might not be 0. So:

i = 6
while (i > 0):
    i = i - 3
print (i)

I put in the Github for future reference.

It’s one way to do it, but it’s much simpler to do it without the tie. I understand you’re trying to demonstrate something in the algorithm, but it’s not necessary, it’s good to be clear that.

Obviously there are minor differences, like the name of the variable that changes nothing, and in the first impression has a text beyond the value.

  • thank you so much!

  • @Vivianjunqueira see in the [tour] the best way to say thank you.

  • in fact, I wanted to understand why one gave the final result and the other gave the results as the repetition... but I already understood that the print commands it! was not seeing this. When I meant that they were the same it was because the structure to me was similar, again I realized that they are not! muitissimo thanks :)!

1

The python only understands that the print (i) is part of while if it is within the scope of the loop. Try to ident the print so that it stays within the scope of while thus:

x = 1
while (x < 4):
    x = x + 1
    print ("x vale", x)

    # Output
    # x vale 2
    # x vale 3
    # x vale 4

0

Hey, how you doing? Within Python, each space serves for indentation, which is used to define structures. For example, if I want to show a count from 0 to 10 in Python, I do this:

i = 0
while i<10:
   i += 1 #mesma coisa que i = i + 1
   print(i)

If I did without the space in "print(i), he would understand that this line will only be executed when the structure ends, then it would only show the value 10. Detail: it is good to be careful with indentation, because it should be the same for all commands of the same structure. For example, if you do a repeat of this, but give a couple of spaces unintentionally in one of the commands, he will understand that one of them is out of indentation, and will give an error message. I don’t know I clarified your doubts, but I hope I helped :-)

Browser other questions tagged

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