totalcount1def average(nums)2 total = 03 count = 14 for n in nums:5 total = totl + n6 count = count + 178 return total / count9print(average([2, 4, 6]))n
Code card: avg.py; the highlighted line is the one the step is aboutTerminal card: what Python prints (errors, tracebacks, results)Memory boxes: the variables a debugger shows when the program pausesNote card and duck: the answer worked out by hand, and a patient listenerEach step edits at most one line of avg.py (the text crossfades) and runs it. An error token hops from the failing line to the terminal card; a value token hops from a line to what it prints. A syntax error stops Python before anything runs; a NameError crashes it mid-run with a traceback, read from the bottom; a logic error prints 3.0 instead of 4.0. Printing values, a breakpoint with the debugger's memory boxes, and explaining the code to the duck point at line 3: count starts at 1. Changing that one line gives 4.0. The terminal card is shortened. Python prints the full path of the file, repeats each line of code under its "File" line, and since Python 3.11 marks the exact spot with ^ and ~ characters.

Three kinds of bug

  1. Python reads the whole file before it runs any of it. Line 1 is missing its colon, which breaks the rules of the language, so nothing runs at all. Python points at the spot: add the colon.

Hunt it down

Fix and check

What you would see

;; step 1/10 · Syntax error: no start
$ python3 avg.py
  File "avg.py", line 1
    def average(nums)
                     ^
SyntaxError: expected ':'

The terminal card is shortened. Python prints the full path of the file, repeats each line of code under its "File" line, and since Python 3.11 marks the exact spot with ^ and ~ characters. Since Python 3.10 a NameError for a near-miss name ends with a hint, here: Did you mean: 'total'?