Question

I'm making a program and I got into a problem. I have a thread running which has a while loop that checks if a global variable is equal to False, if its equal to True then exit the while loop. The problem is even if i update the global variable to True it still doesn't stop, it just continues on.

Code:

While loop:

while stopIt==False:
    print(stopIt) # Always prints out False, even when exit() is called
    # do things...

Stopper:

def exit():
    stopIt = True

stopIt variable defenition:

global stopIt
stopIt = False
Was it helpful?

Solution

The global declaration must be inside the function where you modify the global variable:

def exit():
    global stopIt
    stopIt = True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top