
This is one of Python’s trickiest errors, known as UnboundLocalError. It happens because of how Python handles Variable Scope (where variables can be seen).
โก Quick Fix: UnboundLocalError: local variable referenced before assignment โ Python Variable Scope and global Keyword Fix
Python saw an assignment to score inside your function and classified it as a local variable โ then crashed when you tried to read it before that assignment ran.
# WRONG โ Python treats score as local the moment it sees score = inside the function
score = 0
def increase_score():
score = score + 10 # UnboundLocalError: score referenced before assignment
print(score)
increase_score()
# FIX 1 โ global keyword (quick fix, use with caution)
score = 0
def increase_score():
global score
score = score + 10
print(score)
increase_score() # Output: 10
# FIX 2 โ pass in and return (the right way)
def increase_score(score):
return score + 10
score = 0
score = increase_score(score)
print(score) # Output: 10The explanation below shows exactly why Python makes this decision and when global is appropriate versus when to use arguments and return values instead.
The Scenario
You have a variable outside a function, and you try to change it inside the function. This is where the UnboundLocalError often appears.
Problem Code:
score = 0
def increase_score():
# We try to use the 'score' from outside
score = score + 10
print(score)
increase_score()
# CRASH! UnboundLocalError: local variable 'score' referenced before assignmentWhy it fails
When Python sees score = ... inside the function, it assumes score is a new local variable just for that function. But then it sees the right side of the equation: = score + 10. It tries to find this “new local” score, realizes it hasn’t been created yet, and crashes, leading to an UnboundLocalError.
The Fix: The global Keyword
You must explicitly tell Python: “I’m not making a new variable; I want to use the global one.”
score = 0
def increase_score():
global score # <--- This is the magic line
score = score + 10
print(score)
increase_score()
# Output: 10 (Success!)Pro Tip: Try to avoid global variables if possible! It’s usually better to pass the value in as an argument and return the new value, which helps in preventing an UnboundLocalError.
UnboundLocalError: local variable referenced before assignment โ Scope Rules That Prevent This for Good
UnboundLocalError: local variable referenced before assignment fires because Python makes a single pass through your function before running it. The moment Python spots any assignment to a variable name inside a function โ score = anything โ it marks that name as local for the entire function. Read it before the assignment line runs, and Python crashes.
global fixes the immediate crash. Use it when you have a genuine application-level state variable โ a counter, a flag, a config value โ that multiple functions need to modify directly.
Arguments and return values fix the architecture. Pass the current value in as a parameter, compute the new value, return it. The caller stores the result. This pattern keeps functions self-contained, testable, and free of hidden dependencies on global state.
The third option worth knowing: nonlocal. If your function lives inside another function and needs to modify the outer function’s variable โ not a module-level global โ use nonlocal instead of global. Same syntax, tighter scope.
def outer():
count = 0
def inner():
nonlocal count
count += 1
inner()
print(count) # Output: 1
Pick global for module-level state. Pick nonlocal for nested functions. Pick arguments and return values for everything else.





