Raised when a local variable is referenced before it has been assigned.
An UnboundLocalError occurs when you try to use a local variable in a function before assigning a value to it. This often happens when a variable with the same name exists in the global scope, but you assign to it inside a function without declaring it as global; Python then treats it as a new local variable, which hasn't been given a value yet.
x = 10
def foo():
print(x) # UnboundLocalError here
x = 5
foo()
x = 10
def foo():
global x
print(x) # Works fine
x = 5
foo()