0

I'm trying to implement custom validation... My goal is that if the user's response is not "Y", or not "N", or not "Q", they loop back to the top. Otherwise break.

Both options I've tried will continue to loop even when the the correct response is given.

Here's what I've tried: Option 1:

"""  Use custom validation.  """
while True:
    n_put = input('Would you like to perform a new Google image search?' + user_options())
    if n_put is not "Y" or not "N" or not "Q":
        print('Invalid response. Please read the prompt carefully. ')
    else:
        break

Option 2:

"""  Use custom validation.  """
while True:
    n_put = input('Would you like to perform a new Google image search?' + user_options())
    if n_put is not any(["Y", "N", "Q"]):
        print('Invalid response. Please read the prompt carefully. ')
    else:
        break
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
N.Thompson
  • 45
  • 7
  • 2
    `if not n_put in ['Y', 'N', 'Q']:` or `if not n_put == 'Y' and not n_put == 'N'`... – cwallenpoole Oct 14 '17 at 01:19
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Oct 14 '17 at 04:58
  • @wwii , my question refers to multiple not-conditions. Not so much for the loop. – N.Thompson Oct 14 '17 at 05:01
  • My bad.. possible duplicate of [How do I test one variable against multiple values?](https://stackoverflow.com/q/15112125/2823755) – wwii Oct 14 '17 at 05:02

1 Answers1

0

Correct Option1:

while True:
    n_put = input('Would you like to perform a new Google image search?' + user_options())
    if not (n_put == "Y" or n_put == "N" or n_put == "Q"):
        print('Invalid response. Please read the prompt carefully. ')
    else:
        break

and correct Option2:

while True:
    n_put = input('Would you like to perform a new Google image search?' + user_options())
    if n_put not in ["Y", "N", "Q"]:
        print('Invalid response. Please read the prompt carefully. ')
    else:
        break
SatanDmytro
  • 537
  • 2
  • 7