Skip to content

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.

Whiteboard video lesson: Trace tables and nested loops

20-minute video lesson

Teaching, worked examples and 20 practice questions solved step by step.

Watch free for a month

What 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

  1. Make a column per variable.
  2. Go line by line.
  3. Check each condition.
  4. 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?

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

  1. 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.)

  1. Step 1: 100 → 50 → 25 → 12 → 6

Answer: 100, 50, 25, 12, 6