Python UnboundLocalError: meaning and fix
UnboundLocalError: You read a variable inside a function before it had a value.
Code that causes it
count = 0
def add():
count = count + 1
add()
Open in the runnerYou tried to change an outside variable inside a function. Writing = inside a function makes Python treat it as a new variable.
Take the value as an argument and return the result. If you must, declare it with global inside the function.
How to fix it
count = 0
def add(c):
return c + 1
count = add(count)
print(count)
Open in the runnerWhy it happens
Writing count = inside a function makes that count a new local variable. Reading a new variable with no value stops.
The official docs are in English. See it in the official Python docs