Commenting out code to isolate a bug

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

Commenting out code to isolate a bug

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A line of Python starts with # in front of it. What happens when the program runs?
A Python runs the line and prints a warning
B Python skips the line, so it is temporarily disabled
2. Your program crashes and you suspect the line total = price / qty. Which approach isolates the bug?
A # total = price / qty
B del total = price / qty
3. Why comment out a line instead of deleting it while hunting for a bug?
A You can restore it later by removing the #
B Python fixes commented-out lines automatically
4. You want to disable a block of five lines at once. What is the quickest method in most editors?
A Select the block and use the editor's comment shortcut
B Type # once at the top of the block only
5. Which block is fully commented out?
A # x = 5 y = x * 2 print(y)
B # x = 5 # y = x * 2 # print(y)

Match the Pairs

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

Left columnRight column
1# total = total + price AA block of lines commented out one by one, so none of them run while the bug is being isolated.
2# print(average) # print(count) BThis line is commented out, so Python skips it and does not add the price to the total.
3Restore disabled code after testing by removing the # from the front of the line. Ctotal = total + price
4Comment out the suspect line to see if the bug disappears when that line does not run. DThe # is placed after the code, so the line still runs. The # must go at the start of the line to disable it.
5total = total + price # print(total) EOnly the for line is commented out, leaving an indented line that causes an IndentationError. Comment out the whole block.
6# for item in prices: total = total + item F# result = 10 / count

Definitions

# (commenting out)

Fill in the Blanks

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

1.
_ print(total / count)
2.
_ for n in nums: # total += n print(len(nums))
3.
total = 10 count = 2 _____(total / count)
4.
name = "Sam" _ age = int("twelve") print(name)
5.
if score > 50: _ print("Pass") print("Done")

Spot the Error

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

Question 1
A scores = [8, 9, 7]
B total = sum(scores)
C // average = total / 0
D print(total)
Question 2
A x = 10
B y = x / 0 #
C print(x)
Question 3
A count = 3
B # for i in range(count):
C # print(i / 0)
D count -= 1
E print(count)
Question 4
A # bug fixed: code restored
B total = 0
C total += 5
D # print(total)
Question 5
A age = int(input('Age: '))
B /* print(age + '1') */
C print('Done')

Answer Key

Commenting out code to isolate a bug
Section A: Quick Questions
Q1
B — Python skips the line, so it is temporarily disabled
Q2
A — # total = price / qty
Q3
A — You can restore it later by removing the #
Q4
A — Select the block and use the editor's comment shortcut
Q5
B — # x = 5 # y = x * 2 # print(y)
Section B: Match the Pairs
Q1
# total = total + price → This line is commented out, so Python skips it and does not add the price to the total.
Q2
# print(average) # print(count) → A block of lines commented out one by one, so none of them run while the bug is being isolated.
Q3
Restore disabled code after testing by removing the # from the front of the line. → total = total + price
Q4
Comment out the suspect line to see if the bug disappears when that line does not run. → # result = 10 / count
Q5
total = total + price # print(total) → The # is placed after the code, so the line still runs. The # must go at the start of the line to disable it.
Q6
# for item in prices: total = total + item → Only the for line is commented out, leaving an indented line that causes an IndentationError. Comment out the whole block.
Section C: Definitions
Q1
# (commenting out) — Putting # in front of a line, or a selected block, stops Python running it. Use this to isolate a bug, then remove the # to restore the code.
Section D: Fill in the Blanks
Q1
# print(total / count)
Q2
# for n in nums: # total += n print(len(nums))
Q3
total = 10 count = 2 print(total / count)
Q4
name = "Sam" # age = int("twelve") print(name)
Q5
if score > 50: # print("Pass") print("Done")
Section E: Spot the Error
Q1
C — // is not a Python comment, so Python tries to run it and raises a SyntaxError. To disable the suspect line, put # at the start: # average = total / 0.
Q2
B — A # only disables the text after it. Here the code comes first, so y = x / 0 still runs and causes a ZeroDivisionError. Put the # at the start of the line instead.
Q3
D — When commenting out a block, every line of it needs a #. This indented line was left active, but its for header is a comment, so Python raises an IndentationError. It should be: # count -= 1.
Q4
D — After isolating the bug, the # must be removed to restore the code. With the # still there, print(total) never runs and nothing is shown. It should be print(total).
Q5
B — /* */ is a comment style from other languages, not Python. Python would raise a SyntaxError. Use # at the start: # print(age + '1').