The course
Debugging Python is an intermediate Coding course in seven lessons, after Python from zero and Fix the bug. The scripts belong to Nereus, a deep sea probe, and every lesson starts with one of them broken: a real bug that crashes it or quietly gets a number wrong. You find it with the lesson's technique, fix it, and the check proves the fixed output. The circuits are gallery Pi builds: two lamps, a PIR standing in for the sonar, a button and a lamp, and the traffic light module.
- Reading a traceback: a depth script that crashes, read from the
bottom up; a bad reading in the data, a misspelled name, a missing dict
key, and bad serial input caught with
except ValueErrorwhiletraceback.print_exc()keeps the clue. - Print vs logging: a sonar contact lamp that never lights, found with
one well-placed print (a counter reset inside its loop), then the prints
turned into
loggingwith levels, a logger name and line numbers. - Asserts: a ballast tank that goes below empty without a word, an assert that stops it at the first wrong value, the fix, and asserts turned into small tests that catch a teammate's overflow bug.
- Bisecting a bug: a sonar that puts the sea floor at 21,579 m,
found by checking the middle of the pipeline (an operator precedence
bug), then the same halving to find one bad ping in 1,000 in ten tests
and the first bad firmware version in four, the way
git bisectdoes. - Off by one: an index one past the end, a loop that skips the first reading, an ocean zone boundary on the wrong side (lit on the traffic lights) and the fence post problem, all found with one checklist: first, last, how many.
- State bugs: a toggle that crashes because assigning makes a name local, a default list shared by every dive, a backup that empties itself and a record that leaks from one dive into the next.
- Boss: three broken scripts: the finale.
The finale
An hour before a dive, three scripts fail. A surface beacon stays lit instead of flashing (a copy-paste bug, found by reading the loop as the lamp), a dive log summary crashes on its first line (the CSV header, found from the traceback) and the mode button dies on its third press (a counter that never wraps, found with the off-by-one checklist). Each fix is proved on the circuit, and the course ends with a written bug report for each: symptom, cause, fix and proof.
What the Pi engine does differently
The Pi runs MicroPython 1.29, and debugging is where its messages matter. The lessons quote exactly what it prints:
- A traceback lists the file, line and function of each frame, but not the source line or CPython's carets, so you open the line yourself.
- Some messages are worded differently:
unsupported types for __sub__: 'str', 'int',name 'x' isn't defined,invalid syntax for integer with base 10, and aKeyErrorprints its key without quotes. - Assigning to a global without
globalraisesNameError: local variable referenced before assignment, not CPython'sUnboundLocalError, and does not name the variable. loggingis CPython's: the same levels, formats (%(lineno)dtoo) and the same traps, such asbasicConfigdoing nothing after the first message unless you passforce=True.- pytest cannot be installed, so tests are plain functions of asserts, which is all a unit test is.