Menu

Trace Tables

Completing a Trace Table

Completing a trace table means carrying on the record of an algorithm's run from the values already written down, filling in the rest one line at a time. Done carefully, it tells you exactly what the algorithm outputs and where a logic error hides.

Begin at the beginning

Every variable set before the first loop or selection gets its own row, in the order the algorithm sets it. If the table has a line-number column, a line that only assigns a value still gets a row to itself, so the order of events stays clear.

A partly filled table is always continued, never restarted. If the last completed row shows 3 in the turns column and 12 in the credits column, the next row you write works from those two values.

One line at a time

  • Take the lines in order and never skip one, even when the result looks obvious.
  • Write a variable's new value in the row where that line changes it, so every change is visible.
  • When a line asks for input, put the value you were given straight into the variable that receives it, then carry on from the next line.
  • When a condition is tested, use the values showing at that moment and write True or False in the condition column.

For example, once the variable skip holds 4 and limit holds 6, testing whether skip is less than limit records True in that row.

Worked continuation

A fairground ride uses 5 credits a go, and the programmer wants to count how many goes were taken.

credits = 15
turns = 0
reply = input('Go again? ')
while reply == 'yes':
    credits = credits - 5
    turns = turns + 1
    reply = input('Go again? ')
print(turns)

Output when the user types yes, yes, no:

Go again? yes
Go again? yes
Go again? no
2

The first six rows below were supplied, ending with 10 in the credits column and 1 in the turns column. The second yes was typed next, so carrying on from those values finishes the table.

replycreditsturnsreply == 'yes'output
15
0
yes
True
10
1
yes
True
5
2
no
False
2

Practise this