'Variable referenced before assignment error calling function
this is my code
def bv(a):
if a >= 100000:
bv = "1"
elif a >= 50000 and a < 100000:
bv = "2"
elif a >= 20000 and a < 50000:
bv = "3"
elif a >= 10000 and a < 20000:
bv = "4"
elif a > 5000 and a < 10000:
bv = "5"
return bv
but when I call that function, I get this error:
UnboundLocalError: local variable 'bv' referenced before assignment
I don't understand why. I'm a beginner in python
Solution 1:[1]
You need to use proper indentation:
def bv(a):
if a >= 100000:
bv = "1"
elif a >= 50000 and a < 100000:
bv = "2"
elif a >= 20000 and a < 50000:
bv = "3"
elif a >= 10000 and a < 20000:
bv = "4"
elif a > 5000 and a < 10000:
bv = "5"
else:
bv = ""
return bv
The way you wrote it put the return bv outside of the function, and of course no variable bv exists outside of the function!
Also rec is not defined!
If indentation is not the problem, then none of your cases evaluate to true, you might want to add a default case. I added it to the code snipped.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
