-3

In python is there a way to redo a raw input and if statement if the answer is invalid?

So for instance if you ask the user to guess 1 or 2, and they guess 3 you create the additional elif or else to tell the user that the answer is invalid and go through the raw input / if statement again?

Bach
  • 6,145
  • 7
  • 36
  • 61
FortuneFaded
  • 1,259
  • 4
  • 20
  • 28
  • Are there really no duplicates for this when you search for [python input validation](http://stackoverflow.com/search?q=%5Bpython%5D+input+validation) ? – luk32 Apr 30 '14 at 00:53
  • @luk32: they're a new user, they may not know the right term to search on – smci Apr 30 '14 at 00:54
  • @smci This is a bad excuse. I think there is a tour before asking 1st question, IIRC it explains how to use the search bar and how it works. Or do you mean, going from "invalid input", to "input validation" was too big of a step? Sorry to be so harsh but to me it is a case of quite bold case of lack of effort. – luk32 Apr 30 '14 at 00:55
  • @luk32 Actually yeah you're right, there are reasonable answers even for *[python if-statement invalid answer](http://stackoverflow.com/search?q=%5Bpython%5D+if-statement+invalid+answer)* – smci Apr 30 '14 at 00:57

2 Answers2

3

I believe you want something like this:

# Loop until a break statement is encountered
while True:
    # Start an error-handling block
    try:
        # Get the user input and make it an integer
        inp = int(raw_input("Enter 1 or 2: "))
    # If a ValueError is raised, it means that the input was not a number
    except ValueError:
        # So, jump to the top of the loop and start-over
        continue
    # If we get here, then the input was a number.  So, see if it equals 1 or 2
    if inp in (1, 2):
        # If so, break the loop because we got valid input
        break

See a demonstration below:

>>> while True:
...     try:
...         inp = int(raw_input("Enter 1 or 2: "))
...     except ValueError:
...         continue
...     if inp in (1, 2):
...         break
...
Enter 1 or 2: 3
Enter 1 or 2: a
Enter 1 or 2: 1
>>>
  • It may be useful to add a try-except, so non-integer inputs are also handled. – user2357112 Apr 30 '14 at 00:53
  • While your answer is correct, I personally don't consider exception driven code to be terribly pythonic. I would have it test for the input to be either '1' or '2' and then convert to int once a valid input has been given. This may just be my preference though. – aplassard Apr 30 '14 at 01:00
  • @aplassard - Well, you are welcome to your opinion. :) However, my code is actually more pythonic like this. Most Python programmers live by the [EAFP](https://docs.python.org/3.2/glossary.html#EAFP) motto: "Easier to ask for forgiveness than permission." –  Apr 30 '14 at 01:03
0

Use a while statement:

try:
    x = int(raw_input('Enter your number: '))
except ValueError:
    print 'That is not a number! Try again!'
while x != 1 and x != 2:
    print 'Invalid!'
    try:
        x = int(raw_input('Enter your number: '))
    except ValueError:
        print 'That is not a number! Try again!'

This code starts off by taking the necessary input. Then, using a while loop, we check to see if x is 1 or 2. If not, we enter the while loop and ask for input again.

You could also do this:

while True:
    try:
        x = int(raw_input('Enter your number: '))
    except ValueError:
        print 'That is not a number! Try again!'
    if x in [1, 2]:
        break
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76