Menu

Trace Tables

Trace Tables

A dry run means following an algorithm by hand, step by step, without running it on a computer, keeping track of the value held in every variable. A trace table records the dry run: each row shows the values reached at one step, so you can see exactly how the algorithm behaves.

Trace tables show what an algorithm does, what it outputs for given inputs, how many times a loop repeats, and where a logic error sits โ€” a mistake in the algorithm's reasoning, which appears as a value you did not expect. They cannot help with syntax errors, because a program with one of those never runs. Follow the actual lines in order and write down what really happens, not what the program is meant to do.

word = input("Enter a word: ")
for letter in word:
    print(letter)
    if letter == "E":
        print("Cheer!")

Output when the user types BEES:

Enter a word: BEES
B
E
Cheer!
E
Cheer!
S

Here the input BEES arrives in word, and each pass of the loop adds a row showing the letter and the result of the test:

wordletterletter == 'E'Output
BEES
BFalseB
ETrueE, then Cheer!
ETrueE, then Cheer!
SFalseS

The loop repeats four times, once per letter, yet six lines are printed, because each true test prints a second line. The number of iterations and the number of output lines are not the same thing.

What a trace can reveal

  • Sequence, selection and iteration each appear as rows you work through in order, with a condition column recording each test as True or False.
  • Arithmetic with % (remainder) and // (integer division) is worked out fresh on every pass, not just once at the end.
  • A row holding a value you did not expect points at the line responsible; after you change that line, trace again to check the fix.

Practise this