0

I'm looking to use functions within my Python program to make it cleaner and more efficient, Within my function I either return true or false depending on a user's selection. Though in the case they enter an incorrect / invalid response, I'd like to return a return that way asking more questions don't be asked.

Edit:

To be a bit more descriptive; I'd like to re-create this:

def askquestion(question):
    response = input(question, "Enter T or F")
    if response == "T":
        return True
    elif response == "F":
        return False
    else:
        return None 

def askmultiple():
    questionOne = askquestion("Do you fruits?")
    if questionOne == None:
        return # Exit the function, not asking more questions

    questionTwo = askquestion("Do you Apples?")
    if questionTwo == None:
        return # Exit the function, not asking more questions

I want to cut out checking afterwards if it is None and just return return.

martineau
  • 119,623
  • 25
  • 170
  • 301
Modern_Mo
  • 97
  • 8
  • Can you please show the code you have so far so we have an idea of what you are doing – Agent Lu Jul 01 '19 at 23:01
  • "I'd like to return a return" this doesn't make sense. `return` is part of a statement, it is not an object, thus it cannot be returned. you might as well ask "how do I return an `if`" – juanpa.arrivillaga Jul 01 '19 at 23:03
  • This sounds like you want what [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/364696) was asking for. Only reason I'm not immediately marking it a duplicate is that your question is too vague to be sure it's a duplicate. – ShadowRanger Jul 01 '19 at 23:06

3 Answers3

0

When you don't create a return statement at the end of a function that is equal to a sole return and those two are equal to return None call.

So you may organize your code like:

if returned_value is None:
    # do something a
elif returned_value is False:
    # do something else
else:  # value is True
    # do something b
ipaleka
  • 3,745
  • 2
  • 13
  • 33
0

You can try using a while loop to make sure that the user inputs a correct input. For example:

while not response.isdigit():
     response =  input("That was not a number try again")

In this case, while the user input, "response" is not a number the python console will keep asking for a response. For a basic template,

while not (what you want):
    (ask for input again)

I hope this helps you. :)

Agent Lu
  • 104
  • 13
0

Use an exception flow.

def ask_question(prompt):
    """Asks a question, translating 'T' to True and 'F' to False"""
    response = input(prompt)

    table = {'T': True, 'F': False}
    return table[response.upper()]  # this allows `t` and `f` as valid answers, too.

def ask_multiple():
    questions = [
        "Do you fruits?",
        "Do you apples?",
        # and etc....
    ]

    try:
        for prompt in questions:
            result = ask_question(prompt)
    except KeyError as e:
        pass  # this is what happens when the user enters an incorrect response

Since table[response.upper()] will raise a KeyError if response.upper() is neither 'T' nor 'F', you can catch it down below and use that flow to move you out of the loop.


Another option is to write a validator that forces the user to answer correctly.

def ask_question(prompt):
    while True:
        response = input(prompt)
        if response.upper() in ['T', 'F']:
            break
    return True if response.upper() == 'T' else False
Adam Smith
  • 52,157
  • 12
  • 73
  • 112