-1
question = input('Please choose one. add, times, divide, minus')

if (question=='add'):
if (question=='times'):
if (question=='divide'):
if (question=='minus'):

I want to make it so if I enter something else it will ask me to enter it again instead of error

Wooble
  • 87,717
  • 12
  • 108
  • 131
JD245
  • 61
  • 6

3 Answers3

1

Use elif to chain your conditions together; only one matching condition will then be picked:

if (question=='add'):
elif (question=='times'):
elif (question=='divide'):
elif (question=='minus'):

The added advantage is that you can now tack on an else block, to catch the case where the user picked none of the above:

if (question=='add'):
elif (question=='times'):
elif (question=='divide'):
elif (question=='minus'):
else:

You could put your question asking in an endless loop, and use break to step out of that loop, and perhaps use continue to restart the loop from the top:

while True:
    question = input('Please choose one. add, times, divide, minus')

    if (question=='add'):
        # 
    elif (question=='times'):
        #
    elif (question=='divide'):
        #
    elif (question=='minus'):
        # 
    else:
        print('Please enter a valid option!')
        continue

    # we got here, so we must've had a proper input
    break        

Also see the canonical Asking the user for input until they give a valid response question here on Stack Overflow.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • In which case could you reach the `break` statement ? It seems that all non proper/unknown input would fall in the `else` statement – El Bert Feb 05 '15 at 12:05
  • @bvidal: exactly, which uses `continue` to ask again. If you got proper input, the `break` is reached, after handling the input. – Martijn Pieters Feb 05 '15 at 12:06
  • Ok so that avoid having the break statement in every `if/elif` statement. Got it! – El Bert Feb 05 '15 at 12:07
  • @bvidal: exactly, otherwise you'd need 4 of them, one in each branch. – Martijn Pieters Feb 05 '15 at 12:08
  • plus1 for handling that redundant break statements. – ZdaR Feb 05 '15 at 12:10
  • @MartijnPieters this does avoid redundancy, which is nice, but the comment that goes with it is not correct/precise enough - to me at least. Also, depending on what's in the `if` statements this may go against the idea of being explicit against implicit. What's your take on that ? – El Bert Feb 05 '15 at 12:16
  • 1
    @bvidal: Personally, I'd farm out the prompting to a separate function that is given valid input options to check against. It'll only return if the user has given proper, valid input. – Martijn Pieters Feb 05 '15 at 12:27
0

It will continuously ping the user unless he/she enters the correct input ,

while True:
    question = input('Please choose one. add, times, divide, minus')

    if (question=='add'):
        #Do processing
        break
    elif (question=='times'):
        #Do processing
        break
    elif (question=='divide'):
        #Do processing
        break
    elif (question=='minus'):
        #Do processing
        break
    else:
        print "Please enter a valid input !"
ZdaR
  • 22,343
  • 7
  • 66
  • 87
0

To continue asking:

question = ''
while question not in ('add', 'times', 'divide', 'minus'):
    question = input('Please choose one. add, times, divide, minus')

if question == 'add':
     # do something
elif question == 'times':
     # do something
...

Note, that to interrupt the loop the answer must be one of the alternatives.

RickardSjogren
  • 4,070
  • 3
  • 17
  • 26