“Local variable referenced before assignment” error occurs when you try to change the value of variables wrongly with the specified Python internal rules. The article below will explain those rules and help you to fix this error the perfect way.
Why does the error “Local variable referenced before assignment” occur in Python?
Python is an extremely flexible and powerful language. Its flexibility is due to its strict rules, where the scope of variables inside Python is quite complicated, leading to programmers making mistakes.
“Local variable referenced before assignment” is a fairly common error for beginners. The cause of this error is that you are trying to change the value of a variable inside the function but declared outside the function. For example:
x = 2 def plus_one(): x = x + 1 return x print('The value of x is:', plus_one())
An error message when you run this code:
Traceback (most recent call last):
File "code.py", line 7, in <module>
print('The value of x is:', plus_one())
File "code.py", line 4, in plus_one
x = x + 1
UnboundLocalError: local variable 'x' referenced before assignment
In this example, x
is declared outside the plus_one
function. Inside the function, I tried to change the value of x
by incrementing it by one every time the function was called. This is not valid in Python. It needs some more rules to do it. See how to fix it below.
Solutions to fix “Local variable referenced before assignment” error
Solution 1: Adding the keyword global
The global
keyword is a keyword that makes the variable behind it a global variable, applied only when the variable is declared in a non-global scope. This keyword is only used in a non-global scope when you want to change or reassign a value to it. We will fix the error in the above example with the global
keyword as follows:
x = 2 def plus_one(): global x x = x + 1 return x print('The value of x is:', plus_one())
Add a line of code global x
to specify x
as a global variable declared inside the plus_one
function.
Output:
The value of x is: 3
Solution 2: Passing parameters to the function
This is a pretty simple way to solve this problem. We must pass the parameter to the function and assign any value without declaring it first.
Also, with the above example, we pass parameters and call the function as follows:
def plus_one(x): x = x + 1 print('The value of x is:', x) plus_one(1)
Now we need to call the function and give in the desired argument. x
is now in the scope of the function, so there is no need to use the global keyword.
Summary
There are many ways to solve the “Local variable referenced before assignment” error. Although not the best, the above two are the simplest solutions for debugging. Hope it will be helpful for you. Thanks for reading!
Maybe you are interested:
- Message: ‘geckodriver’ executable needs to be in path
- ModuleNotFoundError: No module named ‘requests’
- ModuleNotFoundError: No module named ‘scipy’ in Python

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css