I know this question has been asked by several times but I still can't find the solution for my problem.
I was writing a function to change something outside the function (in this case, a boolean variable). Although it is known that boolean variables are mutable, I still can't change it. This is my code :
def test() :
global cont
do_something = True #Actually it was something else, but for easy reading, I set it as True
if do_something :
cont = False
def main() :
cont = True
while cont :
test()
print "Stop me"
print "HI"
main()
print "HI"
It simply ran into an infinite loop. I know the following code works.
def test() :
global cont
do_something = True #Actually it was something else, but for easy reading, I set it as True
if do_something :
cont = False
cont = True
print "HI"
while cont :
test()
print "Stop me"
print "HI"
Is this something to do with the global label ? I was told that if I set something global, I can use it anywhere in my program. Is this a special case? So, how can I modify my code to be functional (able to change the "cont" variable) Thanks.