- Analyze code to identify logical errors in control structures.
- Use a tracing technique to understand and predict program flow.
- Clearly explain the cause of a bug and propose a working solution.
Minor Assessment: Debugging Logic Lab (5%)
Welcome, detective! A good programmer isn't someone who never makes mistakes; they're someone who is good at finding and fixing them. This lab is your training. You will be presented with three "case files"—small programs that contain a common but critical logic error.
Your Deliverable: A Single Document
You will submit a single document (e.g., a Google Doc, Word file, or PDF). For each of the three case files below, you must provide three things:
- The Problem: A one-sentence explanation of what the bug is.
- The Cause: A one-sentence explanation of why the bug is happening.
- The Fix: The corrected line(s) of Python code.
The Case Files
Case File #1: The Loop That Never Ends
Intended Goal: This program is supposed to count from 1 to 5.
count = 1
while count <= 5:
print(count)
Case File #2: The Grader with Bad Priorities
Intended Goal: This program is supposed to print "A" for grades 90+, "B" for 80-89, and "C" for 70-79.
grade = 95
if grade >= 70:
print("C")
elif grade >= 80:
print("B")
elif grade >= 90:
print("A")
Case File #3: The Off-by-One Repeater
Intended Goal: This program is supposed to print the message "Hello!" exactly 10 times.
for i in range(1, 10):
print(f"Printing message number {i}")
print("Hello!")
The best way to solve these is to do a "manual code trace." Get a piece of paper and pretend you are the computer. Go line by line, keeping track of the value of each variable. When you find that the program's behavior doesn't match what you expected, you've found the bug!