Trace a ticket rule
Quick review (2 minutes)
Imagine a museum that prints its ticket rule on a board.
A block may hold another if statement. The inner statement's blocks are indented one level further, by four more spaces in this course. Python reaches the inner if only when the block that holds it runs. Each else or elif belongs to the if whose line starts at the same depth.
Write the inputs for one test case at the top of a sheet, with the output you expect from the rule in words.
Go down the program one line at a time. At an assignment, record the new value of the name.
At an
iforelifline, work out the condition with the current values and write True or False beside it.If it is True, move into the block beneath it. If it is False, skip every line indented further than that line and continue at the next line that is indented the same amount or less.
After one block of an
ifstatement has run, skip its remainingelifandelsebranches.Compare the output with what you expected. Repeat with further test cases until every block has run at least once.
Moving a line sideways can change which decision it belongs to, and Python accepts both versions. Suppose the whole if member: statement is moved four spaces to the left, so that its first line starts at the margin. It no longer sits inside the outer else block, so Python reaches the test for every visitor, and every member gets the discount. A member aged 3 then gets 0 - 2, and the program displays Ticket: -2 coins. Trace one case for each branch after you change any indentation.
A second slip is tracing only the case you had in mind when you wrote the code. The case you traced proves nothing about a visitor who takes a different branch. Check the list of test cases against the blocks, and add a case for any block that no test reaches.
1 In plain words
Imagine a museum that prints its ticket rule on a board. The rule is invented for this lesson, and the museum program below follows it. Children under 5 enter free. Visitors under 16 pay 6 coins. Everyone else pays 10 coins. Members get 2 coins off any ticket that is not free. A clerk who reads the board for a member aged 16 goes down it line by line and lands on 8 coins. Python reads the program the same way, one condition after another.
The program for that board puts a decision inside a decision. An if statement written inside the block of another is called a nested if. In the museum program two nested if statements belong to the outer else block, so their if and else lines start four spaces in. The blocks of those nested statements start eight spaces in.
To trace a decision is to play the clerk. Write down the inputs, go through the lines in order, work out each condition you reach and note which block runs. A test case is one set of inputs together with the output you expect, worked out from the rule in words before you look at the code.
Remember one sentence: an else belongs to the if it lines up with. To look it up, see section 8 of the Python 3.13 language reference, on compound statements.
2 Picture it
A table of four test cases. Age 3, a member, pays 0. Age 15, not a member, pays 6. Age 16, not a member, pays 10. Age 16, a member, pays 8. The last row is marked: it is the case traced in this lesson.
3 Why
Indentation tells Python which lines belong to which block, and so which if each else answers. An else line pairs with the if line that starts at the same depth. In the museum program the first else is at the margin, so it pairs with if age < 5. The second else is four spaces in, so it pairs with if age < 16.
The outer decision runs first. Only when age < 5 is false does Python enter the outer else block and meet the two decisions inside it. For a child of 3, those inner lines are never reached, so the member discount cannot make a free ticket cost less than nothing.
A program with several branches needs several test cases. One run shows one path through the code. To see every block run at least once, choose inputs that send Python down each path.
4 The rule
A block may hold another if statement.
The inner statement's blocks are indented one level further, by four more spaces in this course. Python reaches the inner if only when the block that holds it runs. Each else or elif belongs to the if whose line starts at the same depth.
5 How
- Write the inputs for one test case at the top of a sheet, with the output you expect from the rule in words.
- Go down the program one line at a time. At an assignment, record the new value of the name.
- At an
iforelifline, work out the condition with the current values and write True or False beside it. - If it is True, move into the block beneath it. If it is False, skip every line indented further than that line and continue at the next line that is indented the same amount or less.
- After one block of an
ifstatement has run, skip its remainingelifandelsebranches. - Compare the output with what you expected. Repeat with further test cases until every block has run at least once.
6 Worked example
A member aged 16
age = int(input())
member = input() == "yes"
if age < 5:
price = 0
else:
if age < 16:
price = 6
else:
price = 10
if member:
price = price - 2
print(f"Ticket: {price} coins")
Standard input
16
yes
Standard output
Ticket: 8 coins
Save the code as museum.py, run it, type 16, press Enter, type yes and press Enter. The trace below follows this run. 16 < 5 is False, so Python runs the outer else block, where the price becomes 10 and the member discount brings it to 8.
A member aged 3
age = int(input())
member = input() == "yes"
if age < 5:
price = 0
else:
if age < 16:
price = 6
else:
price = 10
if member:
price = price - 2
print(f"Ticket: {price} coins")
Standard input
3
yes
Standard output
Ticket: 0 coins
Run the file again and type 3, then yes. 3 < 5 is True, so Python sets the price to 0 and skips the whole outer else block, including the member discount.
A visitor aged 15 who is not a member
age = int(input())
member = input() == "yes"
if age < 5:
price = 0
else:
if age < 16:
price = 6
else:
price = 10
if member:
price = price - 2
print(f"Ticket: {price} coins")
Standard input
15
no
Standard output
Ticket: 6 coins
Run the file once more and type 15, then no. The outer condition is False, the inner condition 15 < 16 is True, and member is False, so the price stays 6.
Trace
| Instruction | Condition | price | Explanation |
|---|---|---|---|
age = int(input()) | — | unset | The user types 16. age is the number 16. |
member = input() == "yes" | — | unset | The user types yes. The comparison gives True, so member names True. |
if age < 5: | 16 < 5 is False | unset | Skip the line indented under it and go to the else at the same depth. |
else: | — | unset | Enter the outer else block, where two more decisions wait. |
if age < 16: | 16 < 16 is False | unset | 16 is a boundary value. < leaves it out, so skip to the inner else. |
price = 10 | — | 10 | The inner else block runs, because the inner condition was False. |
if member: | member is True | 10 | A name that holds True or False can be a condition on its own. |
price = price - 2 | — | 8 | Work out 10 - 2 first, then give price the result. |
print(f"Ticket: {price} coins") | — | 8 | Back at the margin. The program displays Ticket: 8 coins. |
7 The trap
Moving a line sideways can change which decision it belongs to, and Python accepts both versions. Suppose the whole if member: statement is moved four spaces to the left, so that its first line starts at the margin. It no longer sits inside the outer else block, so Python reaches the test for every visitor, and every member gets the discount. A member aged 3 then gets 0 - 2, and the program displays Ticket: -2 coins. Trace one case for each branch after you change any indentation.
A second slip is tracing only the case you had in mind when you wrote the code. The case you traced proves nothing about a visitor who takes a different branch. Check the list of test cases against the blocks, and add a case for any block that no test reaches.
Quick checks
Quick check 1 of 2
Trace the museum program from the worked example for a visitor who types 5 and then no. What does it display, and which conditions did Python work out on the way?
Show the steps and the answer
It displays Ticket: 6 coins. Python worked out age < 5 as False, age < 16 as True and member as False.
Why: 5 is a boundary value, and < leaves it out, so the outer else block runs. 5 < 16 is True, which sets the price to 6. The reply no makes member False, so the discount block is skipped.
Quick check 2 of 2
Trace this program for the replies 40 and then yes. What does it display?
total = int(input())
express = input() == "yes"
if total >= 50:
cost = 0
else:
cost = 4
if express:
cost = cost + 3
print(cost)Show the steps and the answer
It displays 7.
Why: 40 >= 50 is False, so the else block runs and cost is 4. The nested if is inside that block, express is True, and 4 + 3 is 7.
Sources
- Python 3.13 Language Reference, 8 Compound statements and 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 2.1.8 Indentation. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Tutorial, 4.10 Intermezzo: Coding Style. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Tutorial, 4.1 if Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.