← VCE Software Development U4O1

Commenting out code to isolate a bug

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.

Understanding Commenting out code to isolate a bug in VCE Software Development U4O1

What it does: Deleting the # turns the comment back into live code, so Python runs the line again. If the error goes away with the line disabled, that line is the likely cause of the bug. A # only affects its own line. Selecting the block and using the shortcut (for example Ctrl+/) adds # to every selected line. Each line needs its own # to be disabled. In the wrong choice, only the first line is disabled, so the other two lines still run. Commenting out isolates the cause. The program may now be missing a feature, so you still need to fix the line and restore it.

Common mistakes: // 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. 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.

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 125–171) and word search (Modules 115–171).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A line of Python starts with # in front of it. What happens when the program runs?
Python skips the line, so it is temporarily disabledPython runs the line and prints a warning
Matches
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.
Applying
Meanings
Term
# (commenting out)
Meaning
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.
Practice
price = 20
Testing
Gaps
print(total / count)
Check Answer
Spots
2total = sum(scores)
3// average = total / 0

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

The 'Golden Eagle' badge — High Flyer, 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. 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 ✓
Anything after # on a line is ignored by Python. Adding # in front of code is called commenting it out.
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
Putting # in front of the suspect line disables it. If the crash stops, that line is the likely cause. Deleting it loses the original code.
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
Commenting out is temporary. Remove the # and the original code is back. Python never changes or fixes code that is commented out.
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
A # only affects its own line. Selecting the block and using the shortcut (for example Ctrl+/) adds # to every selected line.
5. Which block is fully commented out?
A. # x = 5 y = x * 2 print(y)
B. # x = 5 # y = x * 2 # print(y) ✓
Each line needs its own # to be disabled. In the wrong choice, only the first line is disabled, so the other two lines still run.

Match the Pairs

# total = total + price → This line is commented out, so Python skips it and does not add the price to the total.
# print(average) # print(count) → A block of lines commented out one by one, so none of them run while the bug is being isolated.
Restore disabled code after testing by removing the # from the front of the line. → total = total + price
Comment out the suspect line to see if the bug disappears when that line does not run. → # result = 10 / count
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.
# 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.

Key Term

# (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.

Fill in the Blanks

1. # print(total / count)
Hint: Type the symbol placed at the start of a line to stop Python running it.
2. # for n in nums: # total += n print(len(nums))
Hint: Type the symbol that starts disabling this block of lines. The other lines in the block already have it.
3. total = 10 count = 2 print(total / count)
Hint: The line has been restored by removing the comment symbol. Type the function that displays output.
4. name = "Sam" # age = int("twelve") print(name)
Hint: Disable the crashing line to test whether it is the cause of the bug. Type the symbol that does this.
5. if score > 50: # print("Pass") print("Done")
Hint: Type the symbol that comments out this indented line so the rest of the block still runs.

Spot the Error

Question 1
A scores = [8, 9, 7]
B total = sum(scores)
C // average = total / 0
D print(total)
Line 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.
Question 2
A x = 10
B y = x / 0 #
C print(x)
Line 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.
Question 3
A count = 3
B # for i in range(count):
C # print(i / 0)
D count -= 1
E print(count)
Line 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.
Question 4
A # bug fixed: code restored
B total = 0
C total += 5
D # print(total)
Line 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).
Question 5
A age = int(input('Age: '))
B /* print(age + '1') */
C print('Done')
Line B — /* */ is a comment style from other languages, not Python. Python would raise a SyntaxError. Use # at the start: # print(age + '1').

Student with a class code? Join your class →