[Solved] – Python – Asking the user for input until they give a valid response

Asking the user for input until they give a valid response

Asked on October 19, 2018 in Python.
Add Comment


  • 4 Answer(s)

    Why would you are doing whereas|a short time|a minute|a moment|a jiffy|a short whereas True and so escape of this loop while you’ll additionally simply place your necessities within the while statement since all you wish is to prevent once you’ve got the age?

    age = None
    while age is None:
        input_value = input("Please enter your age: ")
        try:
            # try and convert the string input to a number
            age = int(input_value)
        except ValueError:
            # tell the user off
            print("{input} is not a number, please enter a number only".format(input=input_value))
    if age >= 18:
        print("You are able to vote in the United States!")
    else:
        print("You are not able to vote in the United States.")
    

    This would result in the succeeding:

    Please enter your age: *tomato*
    tomato is not a number, please enter a number only
    Please enter your age: *5*
    You are not able to vote in the United States of america.
    

    This can work since age can ne’er have a price which will not be and therefore the code follows the logic of your “business process”

    Answered on October 19, 2018.
    Add Comment

    Though the accepted answer is wonderful. i might additionally prefer to share a fast hack for this downside. (This takes care of the negative age downside yet.)

    f=lambda age: (age.isdigit() and ((int(age)>=18 and "Can vote" ) or "Cannot vote")) or \
    f(raw_input("invalid input. Try again\nPlease enter your age: "))
    print f(raw_input("Please enter your age: "))
    

    P.S. This code is for python two.x and may be exported to three.x by dynamical the raw_input and print functions.

     

    Answered on October 19, 2018.
    Add Comment

    To edit your code and fix the error:

    while True:
        try:
            age = int(input("Please enter your age: "))
            if age >= 18:
                print(" If You are able to vote in the United States of america!")
                break
            else:
                print("You are not able to vote in the United States of america.")
                break
        except ValueError:
            print("Please enter a valid response")
    
    Answered on October 19, 2018.
    Add Comment

    The simplest way to accomplish this would be to put the input method in a while loop. Use continue when you get bad input, and break out of the loop when you’re satisfied.

    When Your Input Might Raise an Exception

    Use try and catch to detect when the user enters data that can’t be parsed.

    while True:
        try:
            # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
            age = int(input("Please enter your age: "))
        except ValueError:
            print("Sorry, I didn't understand that.")
            #better try again... Return to the start of the loop
            continue
        else:
            #age was successfully parsed!
            #we're ready to exit the loop.
            break
    if age >= 18: 
        print("You are able to vote in the United States!")
    else:
        print("You are not able to vote in the United States.")

    Implementing Your Own Validation Rules

    If you want to reject values that Python can successfully parse, you can add your own validation logic.

    while True:
        data = input("Please enter a loud message (must be all caps): ")
        if not data.isupper():
            print("Sorry, your response was not loud enough.")
            continue
        else:
            #we're happy with the value given.
            #we're ready to exit the loop.
            break
    
    while True:
        data = input("Pick an answer from A to D:")
        if data.lower() not in ('a', 'b', 'c', 'd'):
            print("Not an appropriate choice.")
        else:
            break

    Combining Exception Handling and Custom Validation

    Both of the above techniques can be combined into one loop.

    while True:
        try:
            age = int(input("Please enter your age: "))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
    
        if age < 0:
            print("Sorry, your response must not be negative.")
            continue
        else:
            #age was successfully parsed, and we're happy with its value.
            #we're ready to exit the loop.
            break
    if age >= 18: 
        print("You are able to vote in the United States!")
    else:
        print("You are not able to vote in the United States.")

    Encapsulating it All in a Function

    If you need to ask your user for a lot of different values, it might be useful to put this code in a function, so you don’t have to retype it every time.

    def get_non_negative_int(prompt):
        while True:
            try:
                value = int(input(prompt))
            except ValueError:
                print("Sorry, I didn't understand that.")
                continue
    
            if value < 0:
                print("Sorry, your response must not be negative.")
                continue
            else:
                break
        return value
    
    age = get_non_negative_int("Please enter your age: ")
    kids = get_non_negative_int("Please enter the number of children you have: ")
    salary = get_non_negative_int("Please enter your yearly earnings, in dollars: ")

    Putting It All Together

    You can extend this idea to make a very generic input function:

    def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None):
        if min_ is not None and max_ is not None and max_ < min_:
            raise ValueError("min_ must be less than or equal to max_.")
        while True:
            ui = input(prompt)
            if type_ is not None:
                try:
                    ui = type_(ui)
                except ValueError:
                    print("Input type must be {0}.".format(type_.__name__))
                    continue
            if max_ is not None and ui > max_:
                print("Input must be less than or equal to {0}.".format(max_))
            elif min_ is not None and ui < min_:
                print("Input must be greater than or equal to {0}.".format(min_))
            elif range_ is not None and ui not in range_:
                if isinstance(range_, range):
                    template = "Input must be between {0.start} and {0.stop}."
                    print(template.format(range_))
                else:
                    template = "Input must be {0}."
                    if len(range_) == 1:
                        print(template.format(*range_))
                    else:
                        print(template.format(" or ".join((", ".join(map(str,
                                                                         range_[:-1])),
                                                           str(range_[-1])))))
            else:
                return ui

    With usage such as:

    age = sanitised_input("Enter your age: ", int, 1, 101)
    answer = sanitised_input("Enter your answer: ", str.lower, range_=('a', 'b', 'c', 'd'))

    Common Pitfalls, and Why you Should Avoid Them

    The Redundant Use of Redundant input Statements

    This method works but is generally considered poor style:

    data = input("Please enter a loud message (must be all caps): ")
    while not data.isupper():
        print("Sorry, your response was not loud enough.")
        data = input("Please enter a loud message (must be all caps): ")

    It might look attractive initially because it’s shorter than the while True method, but it violates the Don’t Repeat Yourself principle of software development. This increases the likelihood of bugs in your system. What if you want to backport to 2.7 by changing input to raw_input, but accidentally change only the first input above? It’s a SyntaxError just waiting to happen.

    Recursion Will Blow Your Stack

    If you’ve just learned about recursion, you might be tempted to use it in get_non_negative_int so you can dispose of the while loop.

    def get_non_negative_int(prompt):
        try:
            value = int(input(prompt))
        except ValueError:
            print("Sorry, I didn't understand that.")
            return get_non_negative_int(prompt)
    
        if value < 0:
            print("Sorry, your response must not be negative.")
            return get_non_negative_int(prompt)
        else:
            return value
    Answered on February 12, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.