Reputation:
I'm trying to use another function running in my while loop to break out of it under certain conditions. I have been writing practice code to try attempt it but it doesn't seem to break... it just keeps running infinitely
global test
def is_True():
test = True
for i in range(5):
test = False
print("Run number:",i)
while(test==False):
print("the is_True method hasn't been called yet")
is_True()
print("The is__True method was called")
Upvotes: 4
Views: 324
Reputation: 386
Here is a work around I've seen :
First, allow the method is_True
to have a return type, a boolean, True/False, i.e:
def is_True():
test = True
return test
Then, allow the while loop to reinitialize the variable test to the return type of is_True
i.e :
print("The is_True method hasn't been called yet")
test = is_True()
This is because, I suspect, the variable in the method is_True
is private and only available within that method. Thus it doesn't affect the state of your global variable : test
.
Overall, the program should resemble this:
global test
def is_True():
test = True
return test
for i in range(5):
print("Run number:",i)
test = False
while(test==False):
print("The is_True method hasn't been called yet")
test = is_True()
print("The is_True method was called")
I hope this answers your question, and happy coding!
Upvotes: 0
Reputation: 476
The problem here is that the test
variable is being defined as a local variable in the function is_true() and is not using the global variable test
. The simplest way to solve this would be to tell python to use the global variable test. You do that by changing:
test = True
to
global test
test = True
That is not, however, the best way to go about this. The best way to go about this would be to do what @Vasilis D or @Colin suggest, which would be to return a value from the is_True() function and use that instead:
def is_True():
return True
for i in range(5):
test = False
print("Run number:",i)
while(test==False):
print("the is_True method hasn't been called yet")
test = is_True()
print("The is__True method was called")
Using global variables is, in general, frowned upon because it can create confusing situations where you define two different variables with the same name and are unsure of which one you're modifying.
Upvotes: 1
Reputation: 2652
A lot of the other answers mention how to fix this, but I figured it would also be helpful to explain why this isn't working, to help in the future.
In is_True
, when you assign to test, you're making a new local variable, instead of refering to the global variable named test
. If you want to refer to the global, add global test
to the start of is_True
. This will tell python that you want to refer to the global variable, not make a new local.
Of course, you probably shouldn't be using global variables for this, so a better solution is to return True
from is_True()
and do test = is_True()
.
Upvotes: 1
Reputation: 532268
You put the global
statement in the wrong place. It goes in the function to indicate that test
is not a local variable. A global
statement at the global scope is basically a no-op.
def is_True():
global test
test = True
That said, try to avoid global variables when possible. Have is_True
return True
instead and assign the return value to test
in the calling scope.
def is_True():
return True
while not test:
print("...")
test = is_True()
print("...")
Upvotes: 6
Reputation: 111
Modify the is_True() function:
def is_True():
return True
and, inside the loop, assign it to test:
test = is_True()
Upvotes: 3