Our ticket program will happily sell minus three tickets. This is creative, but the accountant is crying. We need to describe which requests are allowed and choose what happens next.
You need: variables, integer conversion, input, and f-strings. By the end: you can write mutually exclusive branches and test their boundaries. Allow 30–40 minutes.
Questions that produce values
A comparison is an expression that produces a Boolean value: True or False. These are Python values, written without quotes and with capital initials.
ticket_count = 3
print(ticket_count > 0)
print(ticket_count == 0)
print(ticket_count <= 6)
Output:
True
False
True
| Operator | Question |
|---|---|
== | Are these values equal? |
!= | Are they different? |
<, > | Is the left value smaller / larger? |
<=, >= | Is it smaller / larger, or equal? |
ticket_count = 3 assigns a value. ticket_count == 3 asks a question. The extra equals sign changes the meaning completely. The string "False" is text, not the Boolean value False.
Run a block only when its condition is true
Create check_booking.py:
ticket_count = 3
if ticket_count > 0:
print("That is a positive ticket count.")
print("The accountant has stopped crying.")
print("Check finished.")
if introduces a condition. The colon ends the header. The indented lines form its block, which runs only if the condition is true. Use four spaces for each level of indentation. Indentation is part of Python’s structure, not decoration.
With 3, all three messages print. Change the count to 0: only Check finished. prints. That final line is outside the block because it returns to the left margin.
IDLE often inserts indentation after a colon. When you want to leave a block, remove that indentation. If Python reports an IndentationError, compare the alignment of your lines rather than changing the condition at random.
Choose exactly one outcome
Replace the contents of check_booking.py with:
available_seats = 12
ticket_count = int(input("How many tickets? "))
if ticket_count <= 0:
print("Choose at least one ticket.")
elif ticket_count > available_seats:
print(f"Only {available_seats} seats are available.")
else:
print(f"Request accepted: {ticket_count} tickets.")
print("Thanks for checking availability.")
elif means “otherwise, if.” else handles the remaining cases and has no condition. Python checks the conditions from top to bottom. In this chain, it executes only the first matching block; if none matches, it executes else.
We reject invalid counts first, then excessive counts. Reaching else therefore means the count is positive and fits. This makes the successful path easier to trust: we can state what must already be true.
For now, the input must still be integer text. Typing banana fails in int() before the if statement is reached. Checking the value after conversion and handling a failed conversion are different problems.
Why not use three separate if statements?
Separate if statements are independent checks. They can all run. An if / elif / else chain represents alternatives.
Watch what happens here:
ticket_count = 0
available_seats = 12
if ticket_count <= 0:
print("Invalid quantity.")
if ticket_count <= available_seats:
print("Booking accepted.")
Both messages print. Zero is invalid and less than or equal to twelve. The computer has faithfully followed two incomplete rules. It has not developed a personal grudge.
Use independent checks when multiple actions should be possible, such as warning about low stock and separately recording an event. Use a chain when only one outcome should happen.
Combine conditions when it helps
and requires both conditions to be true; or requires at least one; not reverses a Boolean condition.
ticket_count = 3
available_seats = 12
is_valid = ticket_count > 0 and ticket_count <= available_seats
print(is_valid)
answer = input("Do you have a membership? yes/no: ").strip().lower()
is_member = answer == "yes"
print(is_member)
The first output is True. The second is True if you enter yes, including input such as YES after normalisation. Any other answer currently becomes False. That is a design decision, not a universal rule for forms; a stricter form would reject answers other than yes or no.
Be explicit when comparing alternatives. answer == "yes" or "y" does not mean “answer is either spelling”: the second operand is just a nonempty string, which counts as true in a condition. Write answer == "yes" or answer == "y". We will study this general idea of truthiness later.
Your turn: a party-size rule
The cinema accepts online bookings for one to six people. Larger groups must contact the box office. Write a program with these outcomes:
- Zero or a negative number:
Choose at least one ticket. - More than six:
Contact the box office for group bookings. - One through six:
Online booking available.
Before coding, write down the expected result for -1, 0, 1, 6, and 7. Those are boundary tests: they check both sides of the points where behaviour changes.
Show one solution and its reasoning
ticket_count = int(input("How many tickets? "))
if ticket_count <= 0:
print("Choose at least one ticket.")
elif ticket_count > 6:
print("Contact the box office for group bookings.")
else:
print("Online booking available.")
-1 and 0 take the first branch. 7 takes the second. 1 and 6 take the final branch. Using >= 6 in the second condition would incorrectly reject a group of exactly six.
Checkpoint: explain which block runs, why the others do not, and what happens after the chain. In the next lesson, we will combine all four lessons into a ticket desk and check complete outcomes, not just individual expressions.