Input validation of two conditions

时间:2018-04-20 00:40:38

标签: python validation

I'm working on validating inputs in python. This is the code I have so far:

while input('Would you like to continue(y=yes/n=no)') not in ('y'): 
      print ("Sorry, I didnt catch that. Enter again: ")

The code works but I want to use two conditions, one for 'y' and one for 'n'.

1 个答案:

答案 0 :(得分:1)

Try this:

while True:

    ## Get their choicer
    selection = input('Your message')

    ## If yes
    if selection == 'y':
        do_something_for_y()

    ## If no
    elif selection == 'n':
        do_something_for_n()

    ## If neither, this loops to the top and asks again