Skip to content
IGCSE·Tuition
Computer Science · Lesson

Test indentation and branch structure

In Python a single missing space can move a line out of the loop it was meant to be in.

On this page
  1. How does Python decide which lines are inside a block?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

In Python, indentation is part of the logic. The lines indented under if, elif, else, for and while are the lines that belong to them, so moving a line changes what the program does.

This lesson continues Python transition and execution after mapping an algorithm into a Python function.

How does Python decide which lines are inside a block?

A line ending with a colon opens a block. Every line indented one level under it is inside. The first line indented back out is outside, and runs after the block finishes.

For branches, if, elif and else are checked in order, and only the first branch whose condition is True runs. else catches everything left over.

Worked example

Task: return “High” for marks of 70 or more, “Middle” for 40 to 69, and “Low” otherwise.

def band(mark):
    if mark >= 70:
        return "High"
    elif mark >= 40:
        return "Middle"
    else:
        return "Low"

Step 1, choose boundary tests. The edges are 70, 69, 40 and 39.

Step 2, trace each one.

markmark >= 70mark >= 40result
70Truenot checkedHigh
69FalseTrueMiddle
40FalseTrueMiddle
39FalseFalseLow

Because the first branch catches 70 and above, the elif does not need to say “and below 70”.

The mistake to watch for

A common slip is to use separate if statements for choices that should exclude each other.

Mistaken answer:

def band(mark):
    result = "Low"
    if mark >= 70:
        result = "High"
    if mark >= 40:
        result = "Middle"
    return result

For 80, the first if sets “High”, then the second if is also True and overwrites it with “Middle”. The function returns “Middle” for a mark of 80.

The correction is to change the second if to elif, so it is only checked when the first was False. Testing a mark well above the highest boundary, such as 80, exposes this at once.

Indentation can fail in the same way inside loops:

total = 0
for n in [1, 2, 3]:
    total = total + n
    print(total)

This prints 1, 3, 6. If print(total) is moved back one level, it sits after the loop and prints only 6.

Check yourself

1. What is printed by check(15) and by check(3)?

def check(n):
    if n > 10:
        print("big")
    print("done")
Show answer

check(15) prints big then done. check(3) prints only done, because print("done") is not indented under the if, so it always runs.

2. What does sign(0) return?

def sign(n):
    if n > 0:
        return "positive"
    elif n < 0:
        return "negative"
    else:
        return "zero"
Show answer

“zero”. 0 > 0 is False, 0 < 0 is False, so else runs.

3. Why is 40 a good test value for band?

Show answer

It sits exactly on the boundary of mark >= 40. If a student had written > instead of >=, 40 would return “Low” and expose the error.

Where this leads next

Next, learn to tell a crash from a quiet fault in distinguishing a syntax error from a wrong result. Run band with your own test values in the Python reasoning sandbox, or write the same branches in pseudocode and check them in the restricted pseudocode trace trainer.

Our online one-to-one Computer Science tuition can help if branch questions keep slipping at the boundaries.

Questions people ask

How many spaces should I indent?

Use the same number every time, and four spaces is the common habit. Python only needs the lines in one block to match each other. Mixing tabs and spaces in the same block can cause an error, so pick one style and keep it.

When should I use elif instead of a second if?

Use elif when the choices exclude each other, so only one branch should run. A second if is checked even after an earlier branch has run, so both branches can fire. For grade bands, ranges and categories, elif is usually what you mean.

What are boundary values?

They are the inputs at the edge of a condition, such as 40 and 39 when the test is mark >= 40. Most branch mistakes show up there, so test just below, on and just above each boundary.

Updated:

Your next step

If your branches give odd answers on boundary values, a one-to-one teacher can walk through test inputs with you and show which condition is firing.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students