I'm attempting to loop through a function and use a variable defined in the main scope of the program, but for some reason, it's not passed into my function. This is the first time I've used functions and variable scopes in Python, I read through the Python documentation as well as some various posts on here, but couldn't seem to figure out what I did wrong.
The function will be recursive therefore I'm unable to define them in the head of the function else it will just redefine each time. I tried doing what was done in this post in my file, but it doesn't seem to work.
I have both the main + function in one file and defined the variables I wish to use as global inside the function I want to use them in.
lv1Count = 12
lv2Count = 14
lv3Count = 18
lv4Count = 4
AL = []
def opt(target):
global lv4Count
global lv3Count
global lv2Count
global lv1Count
global AL
goal = target
if (goal <= 0 & lv4Count < 0):
pass
if (goal <= 1 & lv1Count < 0):
pass
if (goal == 2 & lv2Count < 0):
pass
if (goal == 3 & lv3Count < 0):
pass
if (goal == 4 & lv4Count < 0):
pass
opt(4)
I replaced all of the if statements with pass to avoid excessive code, but essentially whenever returning something from these statements, the comparison using the counter doesn't work as it's not successfully reading the value of this variable and the functionality doesn't occur.