Pregunta

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
¿Fue útil?

Solución

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

def exit():
    global stopIt
    stopIt = True
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top