In the comments says to have done test of table. But either you do not understand what the table test is, or you do not understand how a code works, both will produce wrong results. To succeed, you must have a full understanding of what you are doing.
Python is essentially a paradigm imperative so it performs actions through mechanisms. Then each step to be performed will occur at a time. I could speak each line, but neither is this, even in a row can have multiple steps that will be executed according to rules of precedence and associativity. By no means something runs out of order. What the imperative code can do is to explicitly change where you are going to go next, if nothing is done it will always be the next step within the rules quoted above or 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 take another action. Just because n
has changed value does not mean that the condition will be evaluated again.
It is possible to do such a language, but it is not considered suitable for the vast majority of cases. You can even create an abstraction that does this or create a code that does this explicitly. It usually does not pay off and it's best to rephrase the code, which you did, and this is a good exercise to understand how the code really works and how to program it properly. Today the vast majority of "programmers" do not know how to program, they do not understand what the code actually does and can not reform their code.
You could end this way:
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, even though the indentation has introduced a bug into it. The first one also seems to have a bug , it asks for a die once and ignores, then asks again.
In fact, your code still has a repetition 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)
See running on ideone . And in Coding Ground . Also I put GitHub for future reference .
So the code gets more DRY and does everything as it should be going out at the moment you want. Obviously I still expect you to do something with cont
and total
, if you do not use them and should not be there.