When you start introducing functions into your code, you’re bound to encounter an UnboundLocalError at some point. This error is raised when you try to use a variable before it has been assigned in the local context.
In this guide, we talk about what this error means and why it is raised. We walk through an example of this error in action to help you understand how you can solve it.
What is UnboundLocalError: local variable referenced before assignment?
Trying to assign a value to a variable that does not have local scope can result in this error:
UnboundLocalError: local variable referenced before assignment
Python has a simple rule to determine the scope of a variable. If a variable is assigned in a function, that variable is local. This is because it is assumed that when you define a variable inside a function you only need to access it inside that function.
There are two variable scopes in Python: local and global. Global variables are accessible throughout an entire program; local variables are only accessible within the function in which they are originally defined.
Let’s take a look at how to solve this error.
An Example Scenario
We’re going to write a program that calculates the grade a student has earned in class.
We start by declaring two variables:
numerical = 36 letter = "F"
These variables store the numerical and letter grades a student has earned, respectively. By default, the value of “letter” is “F”. Next, we write a function that calculates a student’s letter grade based on their numerical grade using an “if” statement:
def calculate_grade(grade): if grade > 80: letter = "A" elif grade > 70: letter = "B" elif grade > 60: letter = "C" elif grade > 50: letter = "D" return letter
Finally, we call our function:
This line of code prints out the value returned by the
calculate_grade() function to the console. We pass through one parameter into our function: numerical. This is the numerical value of the grade a student has earned.
Let’s run our code and see what happens:
Traceback (most recent call last): File "main.py", line 15, in <module> print(calculate_grade(numerical)) File "main.py", line 13, in calculate_grade return letter UnboundLocalError: local variable 'letter' referenced before assignment
An error has been raised.
Our code returns an error because we reference “letter” before we assign it.
We have set the value of “numerical” to 42. Our
if statement does not set a value for any grade over 50. This means that when we call our
calculate_grade() function, our return statement does not know the value to which we are referring.
We do define “letter” at the start of our program. However, we define it in the global context. Python treats “return letter” as trying to return a local variable called “letter”, not a global variable.
We solve this problem in two ways. First, we can add an
else statement to our code. This ensures we declare “letter” before we try to return it:
… elif grade > 50: letter = "D" else: letter = "F" return letter
Let’s try to run our code again:
Our code successfully prints out the student’s grade. This approach is good because it lets us keep “letter” in the local context. In fact, we could even remove the “letter = “F”” statement from the top of our code because we do not use it in the global context.
Alternatively, we could use the “global” keyword to make our global keyword available in the local context in our
def calculate_grade(grade): global letter if grade > 80: letter = "A" elif grade > 70: letter = "B" elif grade > 60: letter = "C" elif grade > 50: letter = "D" return letter
We use the “global” keyword at the start of our function.
This keyword changes the scope of our variable to a global variable. This means the “return” statement will no longer treat “letter” like a local variable. Let’s run our code. Our code returns: F.
Our code works successfully! Let’s try it using a different grade number by setting the value of “numerical” to a new number:
numerical = 73
Our code returns: B.
That’s it! We have fixed the local variable error in our code.
The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared.
You can solve this error by ensuring that a local variable is declared before you assign it a value. If a variable is declared globally that you want to access in a function, you can use the “global” keyword to change its value.
Now you’re ready to solve UnboundLocalError Python errors like a professional developer!