← VCE Software Development U3O1

Debugging with print()

A debug print. Add a line like this to show a variable's value while the program runs, so you can spot where it goes wrong. Put one inside a loop to watch it change, then remove it once fixed.

Understanding Debugging with print() in VCE Software Development U3O1

What it does: The debug print sits straight after the line that changes total, so you can see the result of that calculation. Commenting out or deleting the debug line stops it appearing in the final program's output. A debug print shows you the real value of a variable, so you can see where it stops being what you expected. Python does not need a label, but it makes the output much easier to read when you have several debug prints. Printing values lets you check your guess with evidence. The first place a value looks wrong is close to the bug. Putting a print inside a loop shows the values every time round, so you can see exactly when score goes wrong.

Common mistakes: There is a comma missing between the label and the variable. The debug print must be print("total =", total) so Python knows they are two separate things to show. Python cares about capital letters. The variable is called count, not Count, so the debug print causes a NameError. It should be print("count =", count).

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 156–166) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Your program gives the wrong total. What is a good first debugging step using print()?
Add print("total =", total) to see the value as the program runsDelete the variable total and start again
Matches
print("total = total")
↓
The variable is inside the quotes, so it prints the words 'total = total' instead of the value. Fix: print("total =", total)
Applying
Meanings
Term
print("total =", total)
Meaning
A debug print. Add a line like this to show a variable's value while the program runs, so you can spot where it goes wrong. Put one inside a loop to watch it change, then remove it once fixed.
Practice
total = 0
Testing
Gaps
print("total =", )
Check Answer
Spots
3 total = total + n
4 print("total =" total)

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

The 'Sea Turtle' badge — Scope Navigator, 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 program gives the wrong total. What is a good first debugging step using print()?
A. Delete the variable total and start again
B. Add print("total =", total) to see the value as the program runs ✓
A debug print shows you the real value of a variable, so you can see where it stops being what you expected.
2. Which line is a helpful debug print?
A. print("score =", score) ✓
B. print("here")
Printing a label and the variable's value tells you exactly what the variable holds. A plain word like "here" does not show any values.
3. Why do we put a label like "total =" inside a debug print?
A. So you can tell which value is being shown in the output ✓
B. Because Python will crash without a label
Python does not need a label, but it makes the output much easier to read when you have several debug prints.
4. A while loop adds numbers to total but the answer is wrong. Where should you put the debug print to watch total change?
A. Inside the loop, indented under the while line ✓
B. Before the loop starts, at the very top
A print inside the loop runs every time round, so you can watch the value change step by step. A print before the loop only shows the starting value.
5. Your for loop has this debug print inside it: print("i =", i). What will it show?
A. The value of i only once, after the loop ends
B. The value of i each time the loop repeats ✓
Because the print is inside the loop body, it runs on every repeat and shows each new value of i.

Match the Pairs

print("total =", total) → A debug print that shows the current value of the variable total while the program runs
print("i =", i, "score =", score) → A debug print inside a loop that shows how i and score change on each pass
Show the value of a variable after a calculation to check it is correct → total = total + price print("total =", total)
Once the bug is fixed, remove the debug print or turn it into a comment so the output stays clean → # print("total =", total)
print("total = total") → The variable is inside the quotes, so it prints the words 'total = total' instead of the value. Fix: print("total =", total)
for i in range(5): score = score + i print("score =", score) → The debug print is not indented, so it runs only once after the loop and does not show score changing each time. Fix: indent it inside the loop.

Key Term

print("total =", total)
A debug print. Add a line like this to show a variable's value while the program runs, so you can spot where it goes wrong. Put one inside a loop to watch it change, then remove it once fixed.

Fill in the Blanks

1. total = 0 total = total + 5 print("total =", total)
Hint: Type the variable name whose value you want to see while the program runs.
2. for i in range(3): total = total + i print("total =", total)
Hint: Type the function that shows the variable's value each time the loop repeats.
3. for num in scores: total = total + num print("num =", num)
Hint: Type the loop variable that holds the current item, so you can watch it change.
4. total = total + price # print("total =", total) print(total)
Hint: Type the symbol that turns the debug line into a comment so it no longer runs.
5. while count < 3: print("count =", count) count = count + 1
Hint: Type the keyword that repeats the block while the condition is true, so the debug print runs each time.

Spot the Error

Question 1
A total = 0
B for n in range(1, 4):
C total = total + n
D print("total =" total)
E print("final", total)
Line D — There is a comma missing between the label and the variable. The debug print must be print("total =", total) so Python knows they are two separate things to show.
Question 2
A count = 0
B while count < 3:
C print("count =", Count)
D count = count + 1
Line C — Python cares about capital letters. The variable is called count, not Count, so the debug print causes a NameError. It should be print("count =", count).
Question 3
A price = 5
B qty = 3
C print("qty =", qty
D cost = price * qty
Line C — The closing bracket is missing, which is a syntax error. The debug print should end with a bracket: print("qty =", qty).
Question 4
A total = 0
B for n in [2, 4, 6]
C total = total + n
D print("total =", total)
Line B — A for loop line must end with a colon. Without it the program will not run, so you cannot even see the debug print inside the loop. It should be for n in [2, 4, 6]:
Question 5
A score == 10
B score = score + 5
C print("score =", score)
Line A — A single = stores a value in a variable, but == compares two values. Using == means score is never created, so the debug print would fail. It should be score = 10.

Student with a class code? Join your class →