Register Login

UnboundLocalError: Local Variable Referenced Before Assignment

Updated Feb 09, 2023

The “local variable referenced before assignment” error occurs when you give reference of a local variable without assigning any value.

Example:

v1 = 1
def myfunction():
    if v1 == 0:
        print("Value: Zero")
    elif v1 == 1:
        print("Value: One")
    elif v1 >= 2:
        print("Value: Greater then 1")
    v1 = 0
    
myfunction()

Output:

UnboundLocalError: local variable “v1” referenced before assignment

Explanation:

In the above example, we have given the value of variable “v1” in two places.

  • Outside the function “myfunction()”.
  • And at the end of the function “myfunction()”.

If we assign a value of a variable in the function it becomes local variable to that function, but in the above example we have assigned the value to “v1” variable at the end of the function and we are referring this variable before assigning.

And the variable“v1” which we have assigned at the beginning of the code block is not declared as a global variable.

Solutions:

To avoid an error like “UnboundLocalError: local variable referenced before assignment” to occur, we have to:

  • Declare GLOBAL variable
  • Pass parameters with the function

Declare Global Variable

Code example with global variable:

v1 = 1
def myfunction():
    global v1

    if v1 == 0:
        print("Value: Zero")
    elif v1 == 1:
        print("Value: One")
    elif v1 >= 2:
        print("Value: Greater then 1")
    
myfunction()

Output:

Value: One

Explanation:

As we know if we declare any variable as global then its scope becomes global.

Pass function with Parameters

Code example passing parameters with function:

def myfunction(v1):
    if v1 == 0:
        print("Value: Zero")
    elif v1 == 1:
        print("Value: One")
    elif v1 >= 2:
        print("Value: Greater then 1")
    
myfunction(10)

Output:

Value: Greater then 1

Explanation:

In the above example, as you can see, we are not using a global variable but passing the value of variable “v1” as a parameter with the function “myfunction()”.

Example 2

def dayweek(day):
    if day == 7 or day == 6 or day == 0:
        wd = 'Weekend'
    elif day >= 1 and day <= 5:
        wd = 'Weekday'
    return wd
    
print(dayweek(10))

Output:

UnboundLocalError: local variable 'wd' referenced before assignment

Example 2.1

def dayweek(day):
    if day == 7 or day == 6 or day == 0:
        wd = 'Weekend'
    elif day >= 1 and day <= 5:
        wd = 'Weekday'
    return wd
    
print(dayweek(1))

Output:

Weekday

In the "example2", we have called a function “dayweek()” with parameter value “10” which gives the error but the same function with value “1” which runs properly in “example 2.1” and returns the output as “Weekday”.

Because in the above function we are assigning the value to variable “wd” if the value of variable "day" is the range from (0 to 7). If the value of variable "day" greater than "7" or lower then "0" we are not assigning any value to variable "wd" That's why, whenever the parameter is greater than 7 or less than 0, python compiler throws the error “UnboundLocalError: local variable 'wd' referenced before assignment

To avoid such type of error you need assign the function variable which lies within the range or we need to assign some value like "Invalid Value" to variable "wd" if the value of variable "day" is not in range from (0 to 7)

Correct Example with Exception

def dayweek(day):
    if day == 7 or day == 6 or day == 0:
        wd = 'Weekend'
    elif day >= 1 and day <= 5:
        wd = 'Weekday'
    else:
        wd = 'Invalid Value'
    return wd
    
print(dayweek(22))

 


×