Coding Python
Home›Python Coding Games›Bug Squash

Bug Squash

Eight broken Python functions. Fix each one until the hidden tests pass.

Bug 1

Tests
    Fix the codePython 3
    1

    How to play

    Each level gives you a function that looks right and a short description of what it is supposed to do. Something in it is wrong. Edit the code in the editor, press Run tests, and the hidden assertions run against your version on a real Python interpreter. A test that fails tells you what it expected, so you can work backwards from the symptom — exactly the loop you will use on your own code for the rest of your programming life.

    You do not have to match our fix. Any code that passes every test counts.

    The eight bugs, and why they matter

    1. Inverted condition. n % 2 == 1 where == 0 was meant. One character; completely wrong behaviour.
    2. Off-by-one. range(1, n) stops before n. The single most common loop bug in any language.
    3. Missing return. The function computes the answer and throws it away, so callers get None.
    4. Type clash. "Age: " + age raises TypeError — an f-string fixes it.
    5. Mutable default argument. The default list is created once at definition time and shared by every call.
    6. IndexError. Looping to len(items) + 1 walks one square past the end of the list.
    7. Bad initial value. A maximum finder that starts at 0 silently breaks on all-negative input — the nastiest kind of bug, because it only fails for some inputs.
    8. KeyError. counts[word] += 1 assumes the key already exists; counts.get(word, 0) + 1 does not.

    Every one of these is drawn from our list of 15 common Python errors and how to fix them, which explains the error messages in more depth.

    A debugging method that works

    1. Read the failing test first. It names the input and the expected output — half the diagnosis.
    2. Predict before you run. Say what you think the current code does for that input, then check. When the prediction is wrong, you have found your misunderstanding, not just the bug.
    3. Change one thing at a time. Two simultaneous fixes make it impossible to tell which worked.
    4. Test the boundaries. Empty list, single item, zero, negative numbers — that is where these bugs hide.
    Debug longer programs on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    Frequently asked questions

    What is the Bug Squash game?

    Eight small Python functions, each containing one realistic bug — an off-by-one error, a mutable default argument, a missing return, a KeyError. You edit the code and press Run tests; the tests execute as real Python and show you which ones pass.

    Do the tests really run Python?

    Yes. Your fixed function and the test assertions are executed on CPython compiled to WebAssembly, in a background worker, so a passing test means the code genuinely works.

    Can I see the solution?

    After three unsuccessful runs a Show solution button appears. Use the hint first — the point is the moment of spotting it yourself.

    Which bugs are included?

    Inverted modulo logic, an off-by-one range, a missing return statement, string-plus-int TypeError, the mutable default argument trap, an IndexError from len() + 1, a max-finder that breaks on negative numbers, and a dictionary KeyError when counting.

    Why practise debugging specifically?

    Because it is most of the job. Beginners spend far more time fixing code than writing it, and the bugs here are the ones that actually recur — they come straight from our guide to the 15 most common Python errors.

    ← All Python coding games  Try Robot Maze →