CSC 1001 — Lab 8: Tracing and Fixing Buggy Programs
Intro
You will work with a partner to trace through a small buggy program, agree on what is broken, and figure out why. Then you will each individually attempt a fix and compare your solutions. This lab builds the skill of reading and reasoning about code carefully before changing anything — the same habit you will lean on with any tool, including AI coding assistants, later in the course.
| Section | Mode | What You'll Do |
|---|---|---|
| Understand | Partner | Trace a buggy program together and agree on what's broken. |
| Do | Individual | Attempt your own fix independently. |
| Communicate | Partner | Compare your fix with your partner's and discuss. |
Submit: your Lab 8 Worksheet and a notebook or script (Lab8.ipynb) with your fixed program.
Understand (with your partner)
- Pick one of the 7 programs in the appendix below.
- Trace through the program by hand, line by line, tracking what each variable holds as the program runs. Record your trace on your Lab 8 Worksheet.
- Agree with your partner on what the problem(s) is/are, and note it on your worksheet, before moving on.
- If you can't find the problem right away, step back and ask:
- What is the big picture goal of this program?
- What is the big picture goal of this specific function?
- What should each variable represent at each point in the program?
- Where does what actually happens first stop matching what you expected?
- If you still can't find the problem after several traces, go ahead and run the program. Try to get as far as you can on paper first — tracing by hand is one of the most powerful debugging skills you can build, and it works even when you don't have a computer in front of you.
Do (individually)
- Working on your own now, look back over your trace.
- If you can see the fix immediately, make the change in
Lab8.ipynb. - If you can't fix it immediately, step back through these checkpoints:
- Program: does the overall structure match what the program is supposed to do?
- Function: does each function do exactly one clear job, and does it do that job correctly?
- Conditionals: are the comparisons checking the right thing, in the right direction?
- Variables: does every variable hold what you think it holds at each step?
- Operators: is each operator (
+,-,*,/,==,<,>=,and,or, ...) the one that is actually needed here?
- Once you know what the problem is, the fix itself is usually a small, close-by change — you shouldn't need to rewrite the whole program.
- Use the trace table on your Lab 8 Worksheet to keep track of your work, then implement your fix in
Lab8.ipynb.
Communicate (with your partner)
- Compare your fix with your partner's fix.
- Discuss, and record your answers on your Lab 8 Worksheet:
- Did you both find the same problem? If not, why might you have looked in different places?
- Did you fix it the same way? Is one fix more complete, or more general, than the other?
- Was there a moment where stepping back to the big picture helped you find something a closer read had missed?
Repeat Understand/Do/Communicate
Complete the same steps for at least 3 total buggy programs.
Appendix: Buggy Programs
Choose one program with your partner. Each program is short on purpose — the goal is careful reading, not reading a lot of code.
Program 1: Letter Grade Assigner
def letter_grade(score: int):
if score >= 90
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
else:
return "F"
print(letter_grade(85))Program 2: Order Total Calculator
def total_cost(price: float, quantity: int):
subtotal: float = price * quantity
tax: float = subtotal * 0.05
return subtotal + tax
print(total_cost(19.99, 3))Program 3: Test Score Averager
def average(numbers):
total: int = 0
for n in numbers:
total: int = total + n
return total / len(numbers)
scores: list[int] = []
print(average(scores))Program 4: Greeting Builder
def greet(name: str, age: int):
message = "Hello " + name + ", you are " + age + " years old."
return message
print(greet("Sam", 19))Program 5: Even Number Counter
def count_evens(numbers: list[int]):
count: int = 0
for i in range(1, len(numbers)):
if numbers[i] % 2 == 0:
count = count + 1
return count
print(count_evens([2, 4, 6, 8, 10]))Program 6: Passing Score Checker
def is_passing(score: int):
if score < 60:
return True
else:
return False
print(is_passing(75))Program 7: Shipping Fee Estimator
def shipping_fee(weight: int, distance: int):
base_fee: int = 5
fee: float = base_fee + weight * 0.5
return fee
print(shipping_fee(10, 200))