Debugging a program with print() and trace tables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Debugging a program with print() and trace tables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
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
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
5. Which print() line is the most helpful for debugging?
A print("total after adding:", total)
B print("here")

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

print(variable)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
total = 0 total = total + 5 _____(total)
2.
for i in range(3): total = total + i print("total:", _____)
3.
total = 30 average = total _ 3 print(average)
4.
def add(a, b): return a + b print(___(2, 3))
5.
def double(n): print("n is", _) return n * 2

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A total = 0
B for n in [2, 4, 6]
C total = total + n
D print('total', total)
Question 2
A def double(x):
B return x + 2
C result = double(5)
D print('result', result)
Question 3
A count = 0
B for i in range(3):
C count = count + 1
D print('count =', i)
Question 4
A price = 20
B discount = 5
C final = (price - discount
D print('final', final)
Question 5
A score == 10
B score = score + 5
C print('score', score)

Answer Key

Debugging a program with print() and trace tables
Section A: Quick Questions
Q1
B — Add print(total) inside the loop to see how it changes each time
Q2
A — Recording what each variable SHOULD be at each step, so you can compare it with the printed values
Q3
B — A program runs fine but calculates average = total * count instead of total / count
Q4
A — The bug happened on or before that step, so check the code that updates score
Q5
A — print("total after adding:", total)
Section B: Match the Pairs
Q1
print("total:", total) → Shows the current value of total at this point so you can compare it with your trace table
Q2
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
Q3
A trace table says total should be 6 after the loop, so print total after the loop to check it → print("after loop:", total)
Q4
A logic error: the program runs without crashing but gives the wrong answer, like adding 1 instead of 2 → count = count + 1
Q5
print("total:" total) → Syntax error: a comma is missing between the text and the variable, so Python stops with an error before running
Q6
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
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
total = 0 total = total + 5 print(total)
Q2
for i in range(3): total = total + i print("total:", total)
Q3
total = 30 average = total / 3 print(average)
Q4
def add(a, b): return a + b print(add(2, 3))
Q5
def double(n): print("n is", n) return n * 2
Section E: Spot the Error
Q1
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]:
Q2
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
Q3
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.
Q4
C — The opening bracket is never closed, which is a syntax error. It should be: final = price - discount (or add the closing bracket).
Q5
A — == compares values, but = assigns them. Without the assignment, score doesn't exist and print() causes a NameError. It should be: score = 10