+ 1
I want to learn why this error occurs in "while loop" function of Pythone.Can someone explain to me?
This is what I use " Ticket = 10 while Ticket > 0 : print("AVAILABLE") Ticket = 10-1 " I made a mistake in where the variable should be "Ticket = Ticket-1 " And the result shows a lot of loops of the word"AVAILABLE".Why does that happen?
3 Respostas
+ 5
You have your answer with the Ticket = 10-1.
Every time the loop begins Ticket is told to be 10 - 1, which is of course 9. Ticket is 9 every iteration. It never gets to 0, so it never stops.
If you change to Ticket = Ticket - 1, the loop stores the "old value" of Ticket and removes 1. So the loop will print "AVAILABLE" 10 times.
+ 2
Charlie Brad ,
besides what Ausgrindtube mentioned, 2 other issues should get fixed:
" Ticket = 10 #> remove quotation mark and unindent this line
while Ticket > 0 :
print("AVAILABLE")
Ticket = 10-1 " # > should be Ticket = Ticket -1, remove quotation mark