Year 8 · Computing · Python: loops · Lesson 4
Trace tables and nested loops
Tracing Python code with trace tables, nested loops, counting inner loop runs, and drawing patterns by repeating text.

20-minute video lesson
Teaching, worked examples and 20 practice questions solved step by step.
Watch free for a monthWhat your child will learn
- Complete a trace table
- Explain nested loops
- Count how many times a nested loop runs
- Predict nested loop output
- Use text repetition for patterns
The method
- Make a column per variable.
- Go line by line.
- Check each condition.
- Inner loop finishes first.
Key words: trace table, nested loop, inner loop, outer loop, bug, end, pattern.
Worked examples
The first three practice questions from the lesson, with every step.
Question 1
What goes in the columns of a trace table?
- Step 1: Each variable, plus output
Answer: One column per variable, plus output
Question 2
x = 2, while x < 10: x = x + 3. Trace the values of x.
- Step 1: 2 → 5 → 8 → 11 (stop)
Answer: 2, 5, 8, 11
Question 3
x = 100, while x > 10: x = x // 2. Trace x. (// divides and rounds down.)
- Step 1: 100 → 50 → 25 → 12 → 6
Answer: 100, 50, 25, 12, 6