Restarting a program after every customer is a very slow queue-management strategy. Our cinema needs to handle repeated actions during one run.
You need: conditions, variables, and input from lessons 1–5. By the end: you can explain when a loop repeats, when it stops, and what changes between iterations. Allow 25–35 minutes.
A condition checked repeatedly
An if checks its condition once when execution reaches it. A while checks its condition before every repetition of its block. Each repetition is an iteration.
Save this complete example as countdown.py:
remaining = 3
while remaining > 0:
print(f"Doors open in {remaining}...")
remaining = remaining - 1
print("Doors open!")
Expected output:
Doors open in 3...
Doors open in 2...
Doors open in 1...
Doors open!
This counts down; it does not wait a second between messages. A computer follows the instructions we wrote, not the dramatic pacing we imagined.
Before the first iteration, remaining is 3. After each iteration it becomes 2, 1, then 0. The next check is false, so Python continues below the block. If it started at 0, the block would never run.
Every loop needs a progress story
Without remaining = remaining - 1, the condition above never becomes false. That is an infinite loop. Do not run a deliberately endless version just to prove the point. If you accidentally create one, use IDLE’s Shell → Interrupt Execution, or Ctrl+C in a terminal.
Before running a loop, answer:
- What is checked?
- What can change that result?
- What happens when the loop finishes?
“The customer will eventually get bored” is not a termination condition Python understands.
Let a command stop the program
Create ticket_loop.py. The only commands for now are sell and quit; one sale means one ticket.
available_seats = 3
command = ""
while command != "quit":
command = input("Type sell or quit: ").strip().lower()
if command == "sell":
if available_seats > 0:
available_seats = available_seats - 1
print(f"Sold. Seats remaining: {available_seats}")
else:
print("Sold out.")
elif command != "quit":
print("Unknown command. Use sell or quit.")
print("Ticket desk closed.")
The empty initial string makes the first condition true. input() obtains a new command each time. A quit command makes the next loop check false. The seat count is initialised before the loop, so it survives between customers in this run. Moving that assignment inside the loop would reset availability every time.
Test sell, sell, sell, sell, quit. The reported seat counts should be 2, 1, 0, then Sold out.. quit should not produce an unknown-command message.
A direct exit with break
Sometimes the stopping decision naturally belongs inside the block:
while True:
command = input("Type quit to close: ").strip().lower()
if command == "quit":
break
print("Still open.")
print("Closed.")
break exits the nearest enclosing loop immediately. while True needs an intentional exit like this. It is not automatically better than a condition-based loop; choose the form whose stopping rule is clearest.
Another keyword, continue, skips the rest of the current iteration. We will use it only when it simplifies the flow. Skipping the line that updates a loop’s counter is a classic way to create an accidental infinite loop.
Performance and resource choices
These examples wait for keyboard input rather than repeatedly checking for a change in a tight loop. A busy loop keeps executing checks without waiting or doing useful work; it can occupy a CPU core and waste energy. Later, scheduled jobs and event-driven systems will give us better ways to wait for external work.
Keep constant setup outside a loop when it does not depend on the iteration. Here that also protects correctness. In larger programs, repeatedly opening connections or loading the same file can waste much more time than simple arithmetic.
Your turn
Change ticket_loop.py so it closes automatically after selling the last seat, while still allowing an early quit. Do not add a second seat counter.
Hint and solution
Change the loop header to:
while command != "quit" and available_seats > 0:
Keep the body unchanged. This is a replacement line, not a standalone program. After the third sale, the next condition check fails and Ticket desk closed. prints. Test both immediate quit and three consecutive sales.
Checkpoint: trace the seat count across three iterations. Explain why the initial seat assignment belongs outside the loop, and why a loop that waits for a person is different from one that continuously polls for work.