Local variable referenced before assignment Solved error in Python

Local variable referenced before assignment Solved error in Python

In this tutorial, we will be discussing why UnboundLocalError: local variable referenced before assignment occurs. When we used functions in our code we come across this type of error as we try to use a local variable referenced before the assignment. Here we will try to understand what this error means, why it is raised and how it can be resolved with the help of examples.

What is UnboundLocalError: local variable referenced before assignment?

In python, the scope of the variable is determined depending on whether the variable is defined inside the function or outside. If the variable is declared inside the function then its scope is within the function itself and we can access it inside the function only.

The error UnboundLocalError: local variable referenced before assignment occurs because we try to assign a value to the variable that does not have a local scope. As the global variable has a global scope the user tries to access to use the variable within the function.

There are two types of variables local variables and global variables.

Difference between local variables and global variables

Local VariablesGlobal Variables
local variable can be defined inside the only function.The global variable can be declared both inside and outside the function
It can be accessed within the function itself.It can be accessed from both inside and outside the function or anywhere within the program.
No need to use keywords to declare it is considered local by default.The global keyword is used to declare a global variable.

Example:

The below example code demonstrates the code where the program will end up with the “local variable referenced before assignment” error.

num= 5
def calculate():
    num = num + 1
    print(num)  
calculate()

Explanation:

  1. We have declared the variable num =5.
  2. Defined a function calculate() in which we have increased the num variable by 1 and stored it in the variable itself.
  3. Printed the num value.
  4. Called the function to calculate.

Error Output:

Output 1 of UnboundLocalError: local variable referenced before assignment

Explanation:

What went wrong here?  We have declared the num variable outside the function while the function cannot access it until you declared it global using the global keyword.

Solution

Solution with the global keyword

num=5
def calculate():
    global num
    num = num + 1
    print(num)  
calculate()

Here we initialized the num variable with the global keyword so that it can be accessed inside and outside the function.

Resolved Output:

Output 2 of UnboundLocalError: local variable referenced before assignment

Solution with passing parameter

Here we can pass the parameter to the function so there is no need of declaring the variable as local or global. So the need for variable declaration is resolved.

def calculate(num):
    num = num + 1
    print(num)  
calculate(5)

Resolved Output:

Output 3 of UnboundLocalError: local variable referenced before assignment

Summary

Hence, in this article, we came to know the reasons for the error “UnboundLocalError: local variable referenced before assignment”. We have understood how the use of global keywords and passing the parameter solved this type of error.

For more articles on python keep visiting our website copyassignment.com

Thank you for visiting our website.


Also Read:

Share:

Author: Ayush Purawr