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.
How to debug a Python function
- Reproduce it. Run the failing input and read the whole traceback, bottom line first: the exception type and message usually name the problem exactly.
- 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.
- 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.
- 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.
- Rerun every test, not just the failing one. A fix that breaks a different case was a guess.
The exercises
Easy
- Range calculator missing numbersOff-by-one
- Formatter crashing on valid pricesType
- Security rule bypassing payment checksLogic
- Missing config key crashes the lookupNull and undefined
- Averages losing precisionType
- Text parser returning wrong countsLogic
- String casing remains lowercaseMutation and copying
- Function crashing when no match foundScope and closure
- Retry mechanism failing instantlyInfinite loops
- Array access throwing TypeErrorType
- String checker throwing IndexErrorOff-by-one
- Sorting function returning undefinedMutation and copying
Medium
- Timer getting stuck in infinite loopInfinite loops
- Math utility exceeding call stackMutation and copying
- Formatting crashes when fields are missingNull and undefined
- Substring returns too many charactersOff-by-one
- Students receiving incorrect gradesLogic
- Function hangs when calculating totalsInfinite loops
- Certain years miscalculatedLogic
- Default configurations altered unexpectedlyMutation and copying
- Discount is subtracted as a flat amountLogic
- Function crashing on variable updateScope and closure
- String formatter freezing the appInfinite loops
- Math loop hanging on odd inputsInfinite loops
- Leaderboard calculating wrong winnerType
- Sorting breaking on mixed data typesType
- Retry logic ignoring zero configurationNull and undefined
- Accumulator throwing TypeErrorNull and undefined
- Validator failing on exact boundariesOff-by-one
- Security check allowing invalid dataLogic
- Difference calculation returning invalid numbersLogic
- Total calculation failing with TypeErrorMutation and copying
- The list that remembersScope and closure
- AI Attempt: The Two Sum TrapLogic
Hard
- Search fails for elements at the endOff-by-one
- Different users sharing the same cartScope and closure
- Original config mutated during copyMutation and copying
- List cleaner leaves half the items behindMutation and copying
- Generated functions returning identical resultsScope and closure
- Independent objects sharing dataScope and closure
- Nested loops timing outInfinite loops
- Lookup failing for matching IDsType
- Zero values dropping from metricsNull and undefined
- Iterator crashing on empty resultsNull and undefined
- Data batcher missing the final recordsOff-by-one
- Algorithm missing the final windowOff-by-one
- Function throwing ZeroDivisionErrorLogic
- Grid clones mutating original dataMutation and copying
- The loop variable that changedScope and closure
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.