1.Why should I care?
Logic bugs give wrong answers quietly. Guessing and changing things at random wastes hours and adds new bugs. A scientific method, applied to code, finds the cause reliably, and the test you write afterwards stops it coming back.
- ◇ Apply a systematic method: reproduce, hypothesise, test, fix, keep the test.
- ◇ Use print probes to check a hypothesis about where values first go wrong.
- ◇ Find and fix off-by-one, indentation and unit-mismatch bugs.
2.Predict
readings = [20.5, 21.0, 22.5, 24.0] but a loop uses range(1, len(readings)) and divides by len(readings). What does it print?
Guess first. Being wrong here is how the surprise works.
3.Understand
The method
- Reproduce: find a small input that always shows the bug. 2. Hypothesise: 'I think the first reading is never added.' 3. Test the hypothesis: add a print, or try an even smaller input. 4. Fix the cause, not the symptom. 5. Keep a test that would have caught it.
Print probes
A print just after a suspect line shows the values at that moment: print("i", i, "total", total) inside a loop. If i starts at 1, the hypothesis is confirmed. Remove or comment out probes once the bug is fixed; keep the assert you add.
Three classic logic bugs
Off-by-one: range(1, n) skips index 0; range(n + 1) runs one too far. Indentation: a return inside a for loop returns after the first item. Units: millimetres divided by seconds labelled m/s is 1000 times too big. The first two show in a probe of the loop; the third shows in a units check.
Explain it another way
Like finding which bulb has failed in a string of fairy lights: you test halfway along, then halfway again, rather than replacing bulbs at random.
Debugging is hypothesis testing. Each probe or reduced input should split the possible causes. Fixing without a confirmed cause risks masking the bug; a regression test prevents it returning.
Engineering teams write a short bug report for every serious fault (symptom, cause, fix, test). The project brief this term asks for exactly that, because it shows how you think.
Worked example 1
count_over([48, 52, 55, 49, 61], 50) returns 0 instead of 3. Find the bug.
Reproduce: it always returns 0. Hypothesis: the loop stops early. Probe after the if: it prints once, with r = 48. So the function returns after the first reading: return count is indented inside the for loop. Fix: un-indent it to the for's level. Keep assert count_over([48, 52, 55, 49, 61], 50) == 3.
Common mix-ups
- Debugging means changing things until it works.
✓ Debugging means forming and testing hypotheses until you know the cause, then fixing that cause. - Once it's fixed, the bug is gone for good.
✓ Bugs come back when code changes. A test that would have caught it keeps it gone.
4.Discover
Work through 'Mean too low', 'Count stuck at 0' and 'A very fast robot'. For each: state a hypothesis in one sentence, place a print probe to test it, then pick the faulty line. Write down the probe that convinced you.
Loading the interactive…
Can't use the interactive? Read a description instead
In the bug hunt, 'Mean too low' prints mean: 16.875 instead of 22.0; a probe after line 3 shows i taking 1, 2, 3, never 0, so line 3's range(1, ...) is the bug. 'Count stuck at 0' prints 0 instead of 3; a probe shows line 6 runs once with r = 48, because return is inside the loop. 'A very fast robot' prints [240.0, 260.0, 240.0] m/s for distances in millimetres; the speeds are really 0.24–0.26 m/s.
5.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
readings = [15.0, 22.9, 29.8, 29.6, 19.2]. The buggy code sets total = 0, runs for i in range(1, len(readings)): total = total + readings[i], then calls print(total / len(readings)). What does it print (to 2 decimal places)?
Practice 2
A program gives a wrong total. What is the best first step?
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3
A function returns after checking only the first reading. What is the most likely cause?
Practice 4
distance_mm differences divided by interval_s are printed as m/s and look 1000 times too big. Best fix?
7.Apply it
Take any program you wrote this term, plant a bug in it (or ask a friend to), and find it using the method. Write a four-line bug report: symptom, hypothesis, test that confirmed it, fix. Add the assert that would have caught it.
8.Reflect
Concepts: Testing and debugging