+ 2

While loops

Why when I code while loops they have no output? Because I have a print() statement And there's no out put. My code looks like this: seats = 200 while seats > 200: print("New Ticket") seats = seats - 1

4th Apr 2024, 10:06 PM
froi pnon
froi pnon - avatar
4 Answers
+ 4
Well, to start off, the condition of the while loop is already false when the interpreter gets to it. Seats is not greater than 200, so the loop block never runs. I think you meant that to be “seats > 0” Don’t run the code after fixing that just yet though, because you actually made another mistake further down, and it can get the interpreter stuck forever. The incrementer line that sets seats to a value of one less than what it was before isn’t indented so as to be part of the loop block, so it would only run if the loop was already finished. If you only fix the loop to run while seats is greater than zero, but don’t indent that incrementer to be part of the loop block, seats will never change, and the condition of the loop will *always* be true.
4th Apr 2024, 11:05 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 2
Because you’re coding them not to have output. If you want more information, you gotta give more information. I have no idea what your code looks like atm.
4th Apr 2024, 10:15 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 2
I added more detail
4th Apr 2024, 10:33 PM
froi pnon
froi pnon - avatar
0
The issue here is that your `while` loop condition is incorrect, leading to the loop not executing. In your code, the `while` loop condition is `seats > 200`, which is not true since `seats` is initially set to 200. Thus, the loop never runs because the condition is already False. If you want to print "New Ticket" while `seats` is greater than 0, you should change the condition to `seats > 0`. Here's the corrected version of your code: seats = 200 while seats > 0: print("New Ticket") seats = seats - 1 With this change, each iteration of the while loop will decrement the `seats` variable by 1 until it reaches 0, printing "New Ticket" for each iteration.
11th Apr 2024, 6:42 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar