Debugging with print()

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

Debugging with print()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which line is a helpful debug print?
A print("score =", score)
B print("here")
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
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
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

Match the Pairs

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

Left columnRight column
1print("total =", total) Atotal = total + price print("total =", total)
2print("i =", i, "score =", score) B# print("total =", total)
3Show the value of a variable after a calculation to check it is correct CThe 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.
4Once the bug is fixed, remove the debug print or turn it into a comment so the output stays clean DA debug print inside a loop that shows how i and score change on each pass
5print("total = total") EA debug print that shows the current value of the variable total while the program runs
6for i in range(5): score = score + i print("score =", score) FThe variable is inside the quotes, so it prints the words 'total = total' instead of the value. Fix: print("total =", total)

Definitions

print("total =", total)

Fill in the Blanks

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

1.
total = 0 total = total + 5 print("total =", _____)
2.
for i in range(3): total = total + i _____("total =", total)
3.
for num in scores: total = total + num print("num =", ___)
4.
total = total + price _ print("total =", total) print(total)
5.
_____ count < 3: print("count =", count) count = count + 1

Spot the Error

Shade in the letter of the line that contains an 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)
Question 2
A count = 0
B while count < 3:
C print("count =", Count)
D count = count + 1
Question 3
A price = 5
B qty = 3
C print("qty =", qty
D cost = price * qty
Question 4
A total = 0
B for n in [2, 4, 6]
C total = total + n
D print("total =", total)
Question 5
A score == 10
B score = score + 5
C print("score =", score)

Answer Key

Debugging with print()
Section A: Quick Questions
Q1
B — Add print("total =", total) to see the value as the program runs
Q2
A — print("score =", score)
Q3
A — So you can tell which value is being shown in the output
Q4
A — Inside the loop, indented under the while line
Q5
B — The value of i each time the loop repeats
Section B: Match the Pairs
Q1
print("total =", total) → A debug print that shows the current value of the variable total while the program runs
Q2
print("i =", i, "score =", score) → A debug print inside a loop that shows how i and score change on each pass
Q3
Show the value of a variable after a calculation to check it is correct → total = total + price print("total =", total)
Q4
Once the bug is fixed, remove the debug print or turn it into a comment so the output stays clean → # print("total =", total)
Q5
print("total = total") → The variable is inside the quotes, so it prints the words 'total = total' instead of the value. Fix: print("total =", total)
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
total = 0 total = total + 5 print("total =", total)
Q2
for i in range(3): total = total + i print("total =", total)
Q3
for num in scores: total = total + num print("num =", num)
Q4
total = total + price # print("total =", total) print(total)
Q5
while count < 3: print("count =", count) count = count + 1
Section E: Spot the Error
Q1
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.
Q2
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).
Q3
C — The closing bracket is missing, which is a syntax error. The debug print should end with a bracket: print("qty =", qty).
Q4
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]:
Q5
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.