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.
Have you tried the table test?
– Jefferson Quesado
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.
– Vitor Mendonça
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 ofi
which will be printed?– Jefferson Quesado
It was very enlightening but disappointing. I swore that the code was stopped when the condition was true. Thank you ! Note: 1
– Vitor Mendonça
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.
– anonimo
i = 0 cond = True while cond: i = 1 cond = False i = 2 print(i) the answer is 2
– Vitor Mendonça