Function not changing global variable
Solution 1:
Your issue is that functions create their own namespace, which means that done
within the function is a different one than done
in the second example. Use global done
to use the first done
instead of creating a new one.
def function():
global done
for loop:
code
if not comply:
done = True
An explanation of how to use global
can be found here
Solution 2:
done=False
def function():
global done
for loop:
code
if not comply:
done = True
you need to use the global keyword to let the interpreter know that you refer to the global variable done
, otherwise it's going to create a different one who can only be read in the function.
Solution 3:
Use global
, only then you can modify a global variable otherwise a statement like done = True
inside the function will declare a new local variable named done
:
done = False
def function():
global done
for loop:
code
if not comply:
done = True
Read more about the global statement.