0

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?

1
  • Your code isn't properly indented; could you fix that? Commented Oct 4, 2017 at 18:34

3 Answers 3

4

You can return some(for example bool) value from function check_age(q) and check it.

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":
        return True
    else:
        return False
while True:
    gender = input("Are you a male or female (M/F)? ").lower()
    check_gender(gender)

    age = input("Please enter your age: ")
    if check_age(age):
        break 
Sign up to request clarification or add additional context in comments.

Comments

2

The best way probably is to raise a custom Exception from inside the question functions as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

NUMBER_MEN, NUMBER_WOMEN = 0, 0


class ExitLoop(Exception):

    pass


def check_gender(reply):
    global NUMBER_WOMEN, NUMBER_MEN
    while True:
        if reply == "f":
            NUMBER_WOMEN += 1
            return
        elif reply == "m":
            NUMBER_MEN += 1
            return
        elif reply == "foo":
            raise ExitLoop
        else:
            print("Please answer M or F: ")
            reply = input("Are you a male or female (M/F)? ").lower()


def check_age(reply):
    if reply == "foo":
        raise ExitLoop
    if not 16 <= int(reply) <= 25:
        print("You are not in the age range for this survey")
        return False
    return True


while True:
    try:
        gender = input("Are you a male or female (M/F)? ").lower()
        check_gender(gender)

        age = input("Please enter your age: ")
        inrange = check_age(age)
        if not inrange:
            continue
    except ExitLoop:
        break

I made a few other changes to your code to make it a bit more pythonic. Be careful by the way, the code above fails when the user enters anything but 'foo' or a number in reply to the age question.

By the way, if you want to ask your user a lot of questions, you might want to check out the excellent Click package.

Hope this helps!

Comments

0

You should try this code, is someone put other letter the loop will break

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()
    if gender != "f" and gender != "m":
        break
    else:
        check_gender(gender)

    age = input("Please enter your age: ")
    try:
        ageValue = int(age)
        check_age(age)
    except ValueError:
        print ("You must enter a number")
        break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.