BugHunt

Python debugging exercises

Writing Python is taught everywhere. Reading Python that almost works, and finding the one line that makes it wrong, is mostly learned the hard way. These exercises are that skill on its own: each one is a short function with a single bug, a description of what goes wrong, and tests that pass only when you have fixed it.

They start with the bugs every beginner meets โ€” an index one past the end, a method that returns None, a string that looks like a number โ€” and move on to shared references, scope and the gotchas that still catch experienced developers. Everything runs in your browser through Pyodide, so there is nothing to install.

See the exercisesFilter and search

How to debug a Python function

  1. Reproduce it. Run the failing input and read the whole traceback, bottom line first: the exception type and message usually name the problem exactly.
  2. Find the line, then the value. The last line of your own code in the traceback is where it broke; print the variables used on that line just before it runs.
  3. Compare with what you expected. Work out by hand what each value should be. The first variable that differs is closer to the bug than the line that crashed.
  4. Fix the cause, not the symptom. Wrapping a line in try/except hides the error; ask why the value was wrong in the first place.
  5. Rerun every test, not just the failing one. A fix that breaks a different case was a guess.

The exercises

What you will practise

Questions

Do I need to install Python?

No. The code runs in your browser with Pyodide, a full Python interpreter compiled to WebAssembly. Nothing is sent to a server.

I am a beginner. Where should I start?

With the easy ones below, in any order. Each takes a few minutes, and the hints get warmer one at a time if you are stuck.

Is there a certificate?

Yes, free. Fixing 25 easy Python bugs while signed in earns Python Debugging ยท Level 1, with a verification link for your LinkedIn profile.