0

I don't know why I never thought of this before... but I'm wondering if there's a neater/shorter/more efficient manner of error handling a user input. For example, if I ask the user to enter either "hello" or "goodbye", and they type something else, I need it to tell the user it's wrong and ask again.

For all of coding I've ever done, this is how I've done it (typically the question is better):

choice = raw_input("hello, goodbye, hey, or laters? ") 

while choice not in ("hello","goodbye","hey","laters"):

   print "You typed something wrong!"

   choice = raw_input("hello,goodbye,hey,or laters? ")

Is there a smarter way of doing this? Or should I just stick with how I've had it? This is the method I use for all languages I've written in.

4 Answers 4

4

For a simple script, the way you have it is fine.

For a more complex system, you're effectivey writing your own parser.

def get_choice(choices):
  choice = ""
  while choice not in choices:
      choice = raw_input("Choose one of [%s]:" % ", ".join(choices))
  return choice

choice = get_choice(["hello", "goodbye", "hey", "laters"])
Sign up to request clarification or add additional context in comments.

6 Comments

I like this because it is general, and the prompt is auto generated based on the choices. My only criticism would be I would do ",".join(choices) instead of " ".join(choices). And I would also do while choice not in choices:
Hmm. I can see creating a method being cleaner, but I feel like the only way I'd prefer it is if I passed a function to it so I could use the method for every question I asked. That would definitely be convenient, just... it might take me a little while to figure out and replace lol
As in, I have a lot of user inputs, so I'd rather not have a new method for each one
@user2869231 "I have a lot of user inputs" -- that's the best time to have a general method! :)
@user2869231 This is a general method, so you wouldn't need a new method for each. If you are concerned about the prompt message, you can add an additional string as a parameter that could be the prompt message.
|
1

If you modify your code to always enter the while loop, you only have to have the raw_input on one line.

while True:
    choice = raw_input("hello, goodbye, hey, or laters? ")
    if choice in ("hello","goodbye","hey","laters"):
        break
    else:
        print "You typed something wrong!"

1 Comment

That's true. It might make things a little prettier not having the second input line in the code
1

You can do it with recursion

>>> possible = ["hello","goodbye","hey"]
>>> def ask():
...     choice = raw_input("hello,goodbye,hey,or laters? ")
...     if not choice in possible:
...         return ask()
...     return choice
... 
>>> ask()
hello,goodbye,hey,or laters? d
hello,goodbye,hey,or laters? d
hello,goodbye,hey,or laters? d
hello,goodbye,hey,or laters? hello
'hello'
>>> 

4 Comments

Doesn't this run the risk of reaching the max recursion limit if the user doesn't know how to type the correct input?
yeah thats why usualy you let the user know.
it also seems like it would be counter-productive; making it longer and more complicated lol
you have 2 choises either recursion or while True, i personaly prefer recursion.
0

That's how you do it. Having the options in a list might be prettier though depending on how you're using it.

options = ["hello", "goodbye", "hey", "laters"]
while choice not in options:
    print "You typed something wrong!"

3 Comments

Don't think it is particularly "prettier", but I guess that is a matter of taste. I do like that you construct the list/tuple only once, though.
@Jblasco It depends heavily on the amount of options.
Sorry, @Mattias, completely misread your answer! I thought you meant to say that a list would be prettier than a tuple! Then, just to make things more complicated, I went and said that I do like that you separated the options... xD

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.