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
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:
- A worked example showing how to read a traceback
- Two guided fixes — a typo bug, then an off-by-one bug you'll trace with
print() - Two independent fixes — an
IndexError, then a silent logic bug - An optional stretch bug, if you want to go further
- 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.
New to notebooks? See Using Jupyter Notebooks first.
What's Next
Coming up: Project 1C — Inventory & the Full Game, your final Unit 1 checkpoint.