Debugging
Find and fix bugs by reading a traceback from the bottom up, tracing code with prints and a table of variables, and correcting off-by-one and wrong-condition mistakes.
It opens on
A program crashes with an error on line 4. Is the mistake always on line 4?
Why it matters
Every programmer spends much of their time debugging, and a calm method, read the error, find the line, trace the values, fix one thing and retest, is what separates guessing from fixing; reading a traceback and tracing a loop are standard exam questions.
Good to know first
- Scope and the call stack: each call has a frame, and the newest is on top
- While loops: the condition is checked before every pass
- Testing with assert: a failing test says that a bug exists
Step by step
Reading a traceback
Read a traceback from the bottom up, name the error, follow the frames back to the bad value, and fix the real cause.
The last line says TypeError, unsupported operand types for +, int and str. Above it, the crash was at line 4, total = total + p, in mean_price, called from line 12 in report.
the traceback, frame by frame
A common mix-up
The bug is always on the line the traceback points to.
In fact: That is where Python noticed the problem; the bad value often comes from an earlier line or another function.
Tracing values
Trace a loop with print statements and a table of variables to find an off-by-one bug, and fix it.
The run printed i = 1 with result [], i = 2 with [1], i = 3 with [1, 2], then after the loop i = 4.
the trace
A common mix-up
Adding prints is a beginner's trick that real programmers do not use.
In fact: Tracing values, with prints or a debugger's breakpoints, is how most bugs are found; remove the prints once the bug is fixed.
Wrong conditions
Find a return in the wrong place inside a loop by tracing it, and fix it so the loop checks every item.
Trace it. x is 3, and 3 < 0 is False, so the else runs, which returns False at once. The loop never reaches −1.
A common mix-up
Every if needs an else.
In fact: Here the else was the bug; the False answer belongs after the loop, once every item has been checked.
Try it
A traceback ends with IndexError, list index out of range, at return xs[3] where xs = [7, 8, 9]. What is wrong?Answer
Xs has indexes 0, 1 and 2 only, so xs[3] does not exist
Hint: Count from 0
A loop for i in range(1, n) is meant to add up 1 to n. What does it give for n = 3, and what should the range be?Answer
1 + 2 = 3 instead of 6; it should be range(1, n + 1)
Hint: Range stops before its end
all_positive(xs) has if x > 0, return True, else return False, inside its loop. What does it give for [1, −5], and how is it fixed?Answer
True, wrong; return False as soon as an item is not positive, and return True after the loop
Hint: Which answer needs every item checked?
total starts as 0, an int. What must p be for int + str to happen, and where did it come from?Answer
p is a string such as "3"; it came from prices, which report filled with the price strings from split.
Why did the loop not run with i = 4?Answer
The condition i < 4 is False when i is 4, so the loop stopped before appending 4.
