Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment:

Var1 = 1 

Var2 = 0 

def function(): 

if Var2 == 0 and Var1 > 0: 

print("Result One") 

elif Var2 == 1 and Var1 > 0:

print("Result Two") 

elif Var1 < 1:

print("Result Three") 

Var1 =- 1 

function()

How can I fix this? Thanks for any help!

1 Answer

0 votes
by (106k points)

To fix this unboundLocalError you can pass the parameters rather than relying on Globals see the code below:-

def function(Var1, Var2): 

if Var2 == 0 and Var1 > 0: 

print("Result One") 

elif Var2 == 1 and Var1 > 0: 

print("Result Two") 

elif Var1 < 1: 

print("Result Three") 

return Var1 - 1 

function(1, 1)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 20, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...