A method for debugging

Guessing is slow. A method is faster, and in a debugging interview the method is what they grade.

  1. Reproduce it. Find the smallest input that shows the bug.
  2. Read the traceback bottom up. The last line is the error. The line above it is where it happened. Further up is how you got there.
  3. Look at the real values. print(repr(x)) shows what print(x) hides: quotes around strings, a trailing space, '5' versus 5, None.
  4. Binary search. Comment out half the code, or check a value halfway through. Which half is the bug in? Repeat.
  5. Check your assumptions. "This list is sorted." Is it? Print it.

In a real terminal, breakpoint() stops the program and opens the pdb debugger, where you can inspect variables and step line by line. It doesn't work in the browser, so here you use print.

Run it. int() fails on one value, and the plain print line hides why. The repr line shows it. Fix the parsing so all three values are counted. Then try the Debugging challenges.