Home βΊ Problems βΊ Modifying a variable before it's assigned locally
Modifying a variable before it's assigned locally
π Fix the bug Topic: functions Difficulty: medium Free
What this problem practises
Function bugs are mostly about names and values: positional argument order, default parameters evaluated once at definition time, local versus global scope, and the implicit None returned whenever a function has no return.
This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β no setup, no local interpreter.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
This should increase a counter and print it, but crashes. Fix it.
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The error it raises
Run the broken snippet and Python stops with this exact error: UnboundLocalError: local variable 'count' referenced before assignment. Match it to the Python errors guide, then repair the line that produced it β knowing the message is half the fix.
The snippet
The interactive editor pre-fills exactly this code, broken and ready to repair:
count = 0
def increment():
count += 1
print(count)
increment()Understand the rule
If a function assigns to a name anywhere in its body, Python treats that name as local throughout the function. Reading it before the local assignment therefore raises UnboundLocalError, even when a global variable with the same name exists.
How to trace it
- Scan the whole function for assignments to the name, not just the line that fails.
- Separate the local variable from any global or outer-scope variable with the same spelling.
- Pass the starting value as an argument when the function should transform caller data.
- Use nonlocal or global only when shared state is truly intentional.
Common wrong fixes
- Assuming Python will read the global value until the first local assignment.
- Adding global as a quick fix and making hidden shared state harder to test.
- Renaming only the assignment and leaving a stale reference in the calculation.
Rule to remember
A local assignment changes the scope decision for the entire function. Explicit parameters are usually clearer than reaching into outer state.
π§© Learning path: Functions
Problem 11 of 12 in this path. Work them in order β each one builds on the rule before it.
β Tuple unpacking with functionsNext in path: Mutable default argument trap β
More functions problems
Keep going
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every functions problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.