Quarry School

Trace a ticket rule

Subject: Computing
Level 1 · FoundationsAbout 7 min read3 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    Imagine a museum that prints its ticket rule on a board.

    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.

    Step

    1. Write the inputs for one test case at the top of a sheet, with the output you expect from the rule in words.

    2. Go down the program one line at a time. At an assignment, record the new value of the name.

    3. At an if or elif line, work out the condition with the current values and write True or False beside it.

    4. 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.

    5. After one block of an if statement has run, skip its remaining elif and else branches.

    6. Compare the output with what you expected. Repeat with further test cases until every block has run at least once.

    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.

    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

    AgeMemberPrice3yes015no616no1016yes8

    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.

    Four test cases for the museum rule

    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

    1. Write the inputs for one test case at the top of a sheet, with the output you expect from the rule in words.
    2. Go down the program one line at a time. At an assignment, record the new value of the name.
    3. At an if or elif line, work out the condition with the current values and write True or False beside it.
    4. 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.
    5. After one block of an if statement has run, skip its remaining elif and else branches.
    6. 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

    Python code
    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

    Python code
    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

    Python code
    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

    Trace the museum program for a member aged 16
    InstructionConditionpriceExplanation
    age = int(input())—unsetThe user types 16. age is the number 16.
    member = input() == "yes"—unsetThe user types yes. The comparison gives True, so member names True.
    if age < 5:16 < 5 is FalseunsetSkip the line indented under it and go to the else at the same depth.
    else:—unsetEnter the outer else block, where two more decisions wait.
    if age < 16:16 < 16 is Falseunset16 is a boundary value. < leaves it out, so skip to the inner else.
    price = 10—10The inner else block runs, because the inner condition was False.
    if member:member is True10A name that holds True or False can be a condition on its own.
    price = price - 2—8Work out 10 - 2 first, then give price the result.
    print(f"Ticket: {price} coins")—8Back 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
    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.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8 Compound statements and 8.1 The if statement

    Quick check 2 of 2

    Trace this program for the replies 40 and then yes. What does it display?

    Python code
    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
    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.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8 Compound statements and 8.1 The if statement

    Sources

    1. 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.
    2. Python 3.13 Language Reference, 2.1.8 Indentation. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    3. Python 3.13 Tutorial, 4.10 Intermezzo: Coding Style. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    4. Python 3.13 Tutorial, 4.1 if Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.