I'm trying to have a while loop ask a series of question and restart when the last question is asked. The thing is, I want the user to have the ability to quit by typing a specific word at any question. I also want as little code as possible in the loop, so the if/elif statements lays in functions.
My question is: Can I continue or break a loop from a function?
My code:
def check_gender(q):
if q == "f":
number_women = number_women + 1
elif q == "m":
number_men = number_men + 1
elif q == "foo":
#break the loop
else:
print("Please answer M or F: ")
q = input("Are you a male or female (M/F)? ").lower()
check_gender(q)
def check_age(q):
if not(16 <= int(q) <= 25):
print("You are not in the age range for this survey")
#Jump back to first question here
if q == "foo":
#break the loop
while True:
gender = input("Are you a male or female (M/F)? ").lower()
check_gender(gender)
age = input("Please enter your age: ")
check_age(age)
#And so on with questions
Is this possible?