← VCE Software Development U3O1

Debugging a program with print() and trace tables

Debugging tool: print a variable at key points, then compare each value with your trace table of what it should be. The first mismatch shows where the bug is.

Understanding Debugging a program with print() and trace tables in VCE Software Development U3O1

What it does: Printing at a key point (after the loop) gives you a real value to compare with the expected 6 in the trace table. If they differ, the bug is before this line. A logic error lets the program run but gives the wrong result. A misspelled function name is a syntax or name error, and Python reports it. Testing one function at a time narrows down where the bug is. Changing lots of things at once makes it harder to find.

Common mistakes: A for loop header must end with a colon. This syntax error stops the program before any print() runs. It should be: for n in [2, 4, 6]:. This is a logic error. The trace table says double(5) should give 10, but print() shows 7 because + was used. It should be: return x * 2.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 168–177) and word search (Modules 168–193).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Your function returns the wrong total, but Python shows no error message. What is the best first debugging step?
Add print(total) inside the loop to see how it changes each timeDelete the loop and rewrite the whole program from scratch
Matches
print("total:" total)
↓
Syntax error: a comma is missing between the text and the variable, so Python stops with an error before running
Applying
Meanings
Term
print(variable)
Meaning
Debugging tool: print a variable at key points, then compare each value with your trace table of what it should be. The first mismatch shows where the bug is.
Practice
def add_scores(a, b):
Testing
Gaps
(total)
Check Answer
Spots
1total = 0
2for n in [2, 4, 6]

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Pufferfish' badge — Error Handler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Your function returns the wrong total, but Python shows no error message. What is the best first debugging step?
A. Delete the loop and rewrite the whole program from scratch
B. Add print(total) inside the loop to see how it changes each time ✓
Printing variables at key points shows what the program is really doing. Compare the printed values with what you expected.
2. What is a trace table used for when debugging?
A. Recording what each variable SHOULD be at each step, so you can compare it with the printed values ✓
B. Making the program run faster by storing its variables in a table
A trace table is your plan for the correct values. The first printed value that does not match it shows where the bug is.
3. Which one is a logic error?
A. A program stops straight away because a line says prnt(score)
B. A program runs fine but calculates average = total * count instead of total / count ✓
A logic error lets the program run but gives the wrong result. A misspelled function name is a syntax or name error, and Python reports it.
4. Your trace table says score should be 15 after the 3rd loop, but print(score) shows 12. What does this tell you?
A. The bug happened on or before that step, so check the code that updates score ✓
B. The bug must be in the last line of the program
The first mismatch between the printed value and the trace table shows where to look. Don't jump to the end of the program.
5. Which print() line is the most helpful for debugging?
A. print("total after adding:", total) ✓
B. print("here")
Printing a label and the variable's value tells you what is happening. A plain message only shows the line was reached.

Match the Pairs

print("total:", total) → Shows the current value of total at this point so you can compare it with your trace table
print("in add(): a =", a, "b =", b) → Prints inputs inside one function so you can test that function on its own and narrow down the bug
A trace table says total should be 6 after the loop, so print total after the loop to check it → print("after loop:", total)
A logic error: the program runs without crashing but gives the wrong answer, like adding 1 instead of 2 → count = count + 1
print("total:" total) → Syntax error: a comma is missing between the text and the variable, so Python stops with an error before running
total = 0 print("total:", total) for n in [1, 2, 3]: total = n → Logic error: total = n replaces the value instead of adding to it. Printing after the loop would show 3 when the trace table expects 6

Key Term

print(variable)
Debugging tool: print a variable at key points, then compare each value with your trace table of what it should be. The first mismatch shows where the bug is.

Fill in the Blanks

1. total = 0 total = total + 5 print(total)
Hint: Type the built-in function that shows a variable's value so you can check it against your trace table.
2. for i in range(3): total = total + i print("total:", total)
Hint: Type the variable being updated in the loop, so you can see its value on every pass.
3. total = 30 average = total / 3 print(average)
Hint: Type the operator that fixes this logic error. It should divide to find the average.
4. def add(a, b): return a + b print(add(2, 3))
Hint: Type the name of the one function you are testing on its own to narrow down the bug.
5. def double(n): print("n is", n) return n * 2
Hint: Type the parameter name whose value you want to print and compare with the trace table.

Spot the Error

Question 1
A total = 0
B for n in [2, 4, 6]
C total = total + n
D print('total', total)
Line B — A for loop header must end with a colon. This syntax error stops the program before any print() runs. It should be: for n in [2, 4, 6]:
Question 2
A def double(x):
B return x + 2
C result = double(5)
D print('result', result)
Line B — This is a logic error. The trace table says double(5) should give 10, but print() shows 7 because + was used. It should be: return x * 2
Question 3
A count = 0
B for i in range(3):
C count = count + 1
D print('count =', i)
Line D — The debug print shows the loop variable i instead of count, so you can't compare it with the trace table for count. It should print count.
Question 4
A price = 20
B discount = 5
C final = (price - discount
D print('final', final)
Line C — The opening bracket is never closed, which is a syntax error. It should be: final = price - discount (or add the closing bracket).
Question 5
A score == 10
B score = score + 5
C print('score', score)
Line A — == compares values, but = assigns them. Without the assignment, score doesn't exist and print() causes a NameError. It should be: score = 10

Student with a class code? Join your class →