Difference between algorithms that read numbers until receiving the number 999

Asked

Viewed 123 times

1

What is the difference of these codes?

First code:

cont = 0 
n = 0 
total = 0 
n = int(input("Digite 999 para parar")) 
while n != 999: 
  n = int(input("Digite 999 para parar"))
  cont += 1 
  total += n 
print(total)

Second code:

cont = 0 
n = 0 
total = 0 
n = int(input("Digite 999 para parar")) 
while n != 999: 
  cont += 1 
  total += n 
  n = int(input("Digite 999 para parar")) 
  print(total) 

I know the solution, but I don’t understand why the difference being that in the "FIRST" the while should be stopped at "n = int(input(type 999 for... )" and not add 999 to the total variable being at the beginning of while.

  • 2

    Have you tried the table test?

  • Yes. My doubt is whether when it enters the while loop and the condition to exit is true, it executes all commands or it leaves wherever it is in the loop.

  • Try to do the python equivalent of this pseudo-code: i = 0; cond = true; while (cond) { cond = false; i = 1; } print(i); ... what the value of i which will be printed?

  • 1

    It was very enlightening but disappointing. I swore that the code was stopped when the condition was true. Thank you ! Note: 1

  • It is. What happens in the first code is that when you enter the first value, if you enter a value other than 999, this value will be ignored and a new reading will be made. In addition you will also add the 999.

  • i = 0 cond = True while cond: i = 1 cond = False i = 2 print(i) the answer is 2

Show 1 more comment

1 answer

5


In his comments, he claims to have taken a table test. But either you don’t understand what a table test is, or you don’t understand how a code works, they’ll both produce wrong results. To make it work you need to have complete understanding of what you’re doing.

Python is an essentially paradigm imperative so it executes actions through mechanisms. Then each step to be executed will occur at each time. I could speak every line, but neither is that, even in a row can have several steps that will be executed according to rules of precedence and associativity. By no means is something executed out of order. What the imperative code can do is to change explicitly where it goes next, if nothing is done it will always be the next step within the rules mentioned above or in the next line.

There is no implicit mechanism that with each change of state of the code something will be executed without you noticing to make a decision or make another action. Just because n value change does not mean that the condition will be evaluated again.

It is possible to make such a language, but it is not considered suitable for the vast majority of cases. It’s even possible for you to create an abstraction that does this or create a code that does this explicitly. It usually doesn’t pay and it’s better to rewrite the code, which you did, and it’s a good exercise to understand how the code actually works and how to program it in the most appropriate way. Today the vast majority of "programmers" do not know how to program, they do not understand what the code actually does and fail to reform their codes.

You could end up like this:

cont = 0
n = 0
total = 0
n = int(input("Digite 999 para parar"))
while n != 999:
    n = int(input("Digite 999 para parar"))
    if n == 999: break
    cont += 1
    total += n
print(total)

But I do not advise, your second code is much clearer and simpler, although the indentation introduced a bug in it. The first also seems to have a bug, he asks for a die once and ignores it, then asks again.

Actually your code still has a repeat problem and can be simplified:

cont = 0
n = 0
total = 0
while True:
    n = int(input("Digite 999 para parar"))
    if n == 999: break
    cont += 1
    total += n
print(total)

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

So the code gets more DRY and does everything as it should be going out at the time you wish. Obviously I still hope you do something with cont and total, if you don’t use them you shouldn’t even be there.

  • I’m really learning. It’s a beginner’s exercise. I found it interesting how you did the loop. I was not taught that way, but it is much more pleasant in this example. Thank you very much for the comment !

Browser other questions tagged

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