+ 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?

24th Feb 2025, 6:06 AM
Charlie Brad
Charlie Brad - avatar
3 Antworten
+ 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.
24th Feb 2025, 6:58 AM
Ausgrindtube
Ausgrindtube - avatar
+ 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
24th Feb 2025, 11:26 AM
Lothar
Lothar - avatar