Is it possible to exit a function from a subfunction it calls
Solution 1:
from random import randrange, uniform
def match (x, y):
if x == y:
print("True!")
else:
print("False")
raise Exception()
def main():
try:
while True:
match(randrange(0, 10), randrange(0, 10))
while True:
match(randrange(0, 10), randrange(0, 10))
while True:
match(randrange(0, 10), randrange(0, 10))
except:
print("exit")
main()
This may not be the answer you wanted, but I suppose it could be helpful.
By using try and except to create a scope that include all while
loop, you can exit the function via raising exception.
If you want to end the function entirely (not doing try/except
in function), you can simply do this:
try:
main()
except:
#something else
This also allow you to do it without modifying the function itself.