- Read a short Python program and work out what it does, without running it
- Trace variable values through a program line by line
- Identify which constructs (sequence, selection, iteration) a program uses
- I can predict the output of a program by tracing it
- I can complete a trace table showing how a variable's value changes
- I can explain, in plain English, what a piece of code does and why
Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.
1. What does x = 5 followed by print(x) output?
2. If age = 12, what does if age >= 13: evaluate to?
3. True or false: you can work out what a program will do just by reading it, without running it.
Key vocabulary
Reading and Explaining Code
Why reading code matters
So far, most lessons have focused on writing code. But being able to read code — working out what a program does just by looking at it — is just as important a skill, and it's exactly what SQA assessment expects pupils to demonstrate. A program combines the three basic constructs covered so far: sequence (SDD1–SDD2), selection (SDD3), and iteration (SDD4–SDD5). Reading code well means recognising which of these is happening at each point, and following the values stored in variables as the program runs.
Tracing line by line
A trace table is not a guess. It is a record of what each variable contains after each important line runs.
To trace a program, go through it one line at a time, exactly in the order Python would run it, and keep track of what changes. For sequence, this is simple — just follow the lines in order. For selection, work out whether the condition is true or false, and only follow the branch that matches. For iteration, the same lines get followed multiple times, so keep track of how many passes have happened and what has changed each time.
Trace tables
A trace table is a simple tool for tracing: draw a column for each variable in the program, then add a new row every time a variable's value changes, filling in the new value. This is especially useful for loops, where a variable might change many times before the program finishes.
| Line executed | count | total |
|---|---|---|
count = 0 | 0 | — |
total = 0 | 0 | 0 |
total = total + count (pass 1) | 0 | 0 |
count = count + 1 (pass 1) | 1 | 0 |
total = total + count (pass 2) | 1 | 1 |
Explaining code in plain English
Explaining what code does is different from just describing each line. A good explanation says what the program achieves overall and why, not just a line-by-line translation. For example, rather than "it sets total to 0, then it loops and adds count to total," a stronger explanation says "this program adds up a series of numbers using a running total, built inside a loop."
Worked examples
Code: score = 7 then if score >= 5: / print("Pass") / else: / print("Fail")
score is set to 7.score >= 5 is checked: 7 >= 5 is True.if branch runs: Pass is printed. The else branch is skipped entirely.Code: total = 0 then for number in range(1, 4): / total = total + number, then print(total)
| Pass | number | total |
|---|---|---|
| start | — | 0 |
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
range(1, 4) produces 1, 2, 3.total builds up: 0 → 1 → 3 → 6.6.Code: attempts = 0 then password = input("Enter password: ") then while password != "abc123": / attempts = attempts + 1 / password = input("Try again: "), then print("Correct after", attempts, "attempts")
Code: total = 0 then for n in range(2, 5): / total = total + n, then if total > 8: / print("High") / else: / print("Low")
- Complete a trace table for
nandtotal. - What is printed at the end?
Trace: n takes 2, 3, 4. total goes 0 → 2 → 5 → 9.
Output: 9 > 8 is True, so "High" is printed.
N4 pupils: when asked to explain code, always name the construct being used (sequence, selection, or iteration) as part of your answer — this shows the assessor you can identify constructs, not just describe behaviour.
Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.
1. x = 3 then y = x + 4 then print(y). What is printed? TYPE 1
2. mark = 4 then if mark >= 5: print("Pass") else: print("Fail"). What is printed? TYPE 1
3. total = 0 then for n in range(1, 4): total = total + n. What is total after the loop? TYPE 1
4. A program contains an if / elif / else chain and a for loop. Which constructs does it use? TYPE 1
5. count = 1 then while count < 4: count = count + 1 then print(count). What is printed? TYPE 1
6. Trace this code and state what is printed: x = 2 / y = 0 / for i in range(3): / y = y + x / print(y). Show your trace table. TYPE 2 N3
6.7. Explain, in your own words and without describing every line, what this program does: total = 0 / count = 0 / number = input("Enter a number, or 'done' to finish: ") / while number != "done": / total = total + int(number) / count = count + 1 / number = input("Enter a number, or 'done' to finish: ") / print(total / count) TYPE 2 N4
8. Practical: In IDLE, create a new file called sdd6_tracing.py. Without running it first, predict on paper what this program will print, then type it in, run it, and check your prediction: total = 0 / for i in range(1, 5): / total = total + i / print(total). Record your prediction and the actual output. TYPE 3
10 — matches the prediction.9. Practical: In sdd6_tracing.py, add comments above each line of your program from Q8 explaining what that line does, then add one more comment at the very top explaining the program's overall purpose in one sentence. Paste your commented code below. TYPE 3
# This program adds up the numbers 1 to 4 using a running total
# set up the running total
total = 0
# repeat once for each number 1 to 4
for i in range(1, 5):
# add the current number to the running total
total = total + i
# display the final total
print(total)Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 15 min; now you try 5 min; task set 15 min.
Key misconception to address: pupils narrating code line-by-line instead of explaining its purpose — model both a weak and strong explanation on the board, as in Worked Example 3.
Live demo suggestion: build a trace table together as a class on the board for Example 2 before pupils attempt Now You Try independently.
Extension idea (S5 / N5-bridging, non-assessed): briefly show a trace table for nested selection inside a loop as a look-ahead — keep clearly separate from the assessed task set.
Assessment standards covered: N3 O2.1; N4 O1.1, O1.2, O1.3.