简体   繁体   中英

Logic problem using global variables as flags in python

I am having a problem trying to use a global variable as a flag to determine a condition for a function. I am just starting to learn Python and it's been several years since a used C/C++.

The simplified code is as follows (I don't post the rest of the code because I think that the problem is within these sections, and probably it is a dumb one):

def func1():
    global flag1
    global flag2
    
    if flag1 or flag2:
        if flag1:
            'do something'
            return x
        elif flag2:
            'do something'
            return X
        else:
            pass
    else:
        if 'condition_1 is met':
            flag1 = True        
            'do something'
            return x
        elif 'condition_2 is met':
            flag2 = True
            'do something'
            return x
        else:
            pass


def func2():
    func1()
    window.after(100, func1)


if __name__=='__main__':
    flag1 = False
    flag2 = False

'func2' is always running, therefore 'func1' is also always running because it is called from inside 'func2'.

The error I get is "using flag1 and flag2 variables before assignment", and I think it's because I am trying to change the flags values inside the 'if' statement, but I can't think of another logic solution for this one.

Like I said, it is probably a dumb error and there is a simple solution, so I will be more than thankfull if somebody can help me.

Thanks for the replys. Trying to make a minimal reproducible example I detected that there is nothing wrong with the logic of my script, but I have to dig deeper to find the problem. Sorry for the inconvenience. This answer can be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM