Debugging Basics

Every programmer, at every level, spends serious time fixing code that doesn't work. That's not a sign something's gone wrong with you — it's just what programming actually is. Today's about doing it efficiently: reading error messages properly, and tracing through code by hand when the bug isn't obvious.

What You'll Learn

  • Reading tracebacks bottom-to-top — the last line names the error type and gives the clearest description
  • Error types: NameError (undefined variable), TypeError (wrong type for the operation), IndexError (out-of-range access)
  • Print-statement debugging — adding temporary print() statements to see what a variable actually holds
Watch out for: not every bug crashes your program. A logic bug runs fine and produces a result — just the wrong one. Those are exactly what print-statement debugging is for.

Today's Activity

Today's practice is different — instead of writing new code, you're finding and fixing bugs in code that's already there. Work through the notebook below top to bottom:

  1. A worked example showing how to read a traceback
  2. Two guided fixes — a typo bug, then an off-by-one bug you'll trace with print()
  3. Two independent fixes — an IndexError, then a silent logic bug
  4. An optional stretch bug, if you want to go further
  5. One check-your-understanding question to close

Download the notebook, save it into your 1_foundations folder in your cloned repo, and open it in VS Code. Commit once you've saved it, then commit again when you're done for the day.

Download 1.8_Debugging_Basics.ipynb

New to notebooks? See Using Jupyter Notebooks first.

What's Next

Coming up: Project 1C — Inventory & the Full Game, your final Unit 1 checkpoint.