. Advertisement .
. Advertisement .
“Local variable referenced before assignment” is a common error for Python programs. Yet, not many people know how to tackle this issue. Our guidelines will shed more light on this matter to clear the clouds for you.
How to Fix The Error “UnboundError: Local Variable Referenced Before Assignment”?
1. Why Does UnboundError Occur?
When you try to assign one value to a new variable with no local scope, the following error will happen:
UnboundLocalError: local variable referenced before assignment
Python requires you to always determine the variable scope. Suppose one variable gets assigned in one function; you can say this variable is a local one. Why? Python assumes that once you define variables inside functions, you want to access that variable within that function only.
Two types of variable scopes are available in Python: global and local. Contrary to local, global variables offer access through the whole program, while local ones can only be accessed inside whatever function you originally assigned them.
2. An Example of The Error
Let’s create a program to calculate a student’s total grade in a class. First, we will declare two variables:
numerical = 35 letter = "F"
These two variables contain this student’s letter and numerical grades, respectively. As a default, the “letter” value is “D”.
Next, let’s write the function using “if” statements to measure his letter grade that is based on his numerical one.
def calculate_grade(grade): if grade > 79: letter = "A" elif grade > 69: letter = "B" elif grade > 59: letter = "C" elif grade > 49: letter = "D" return letter
At last, call the function:
This code line prints out values returned from your calculate_grade() function to consoles. Next, pass one parameter into the function: numerical, which is the student’s numerical grade value.
Now run the code to see what will happen:
UnboundLocalError: local variable 'letter' referenced before assignment
As expected, the error takes place.
3. The Solution
The code returns an error since we referenced “letter” before assigning it. To be more specific, we set the “numerical” value to 41, but the “if” statements never set grade values over 50. What does that mean? Whenever we call the
calculate_grade() functions, the return statements are unsure which value we refer to.
It’s true that we did give “letter” a definition before starting the program. Yet, at that time, we defined it within a global context. On the other hand, Python always treats the “return letter” as an attempt to return local variables named “letter” – instead of one global variable.
Here is the best way to tackle this issue. You may add “else” statements to the code, ensuring we have declared “letter” before returning it:
def calculate_grade(grade): if grade > 79: letter = "A" elif grade > 69: letter = "B" elif grade > 59: letter = "C" elif grade > 49: letter = "D" else: letter = "F" return letter
Now run the code again:
Our guidelines have explained, analyzed, and solved the issue “Unbounderror: Local variable referenced before assignment“. We hope you can use this wonderful tip to tackle whatever issue with your program. For similar local errors in Python (such as “unable to get local issuer certificate”), feel free to browse our website for more guidance.