Python Python Basics All Together Now Handle Exceptions

Joel Williams
Joel Williams
800 Points

How to correctly syntax additional choices when asked [Y/N]?

As it stands, the only choices are "y" or "Y" and then any other entry prints the message "Thank you anyways, {}!"

How would I program it to include "YES" and "yes?" Also, since any other option generates the message, how do I create an "if statement" for choosing no AND generate a user friendly message for anything but "y", "Y", "YES", "yes", "n", "N", "NO", "no?"

1 Answer

Steven Parker
Steven Parker
167,746 Points

You could check for a number of possible answers by testing for membership "in" a list of possible choices, and test for both affirmative and negative responses with a default for anything else:

should_proceed = input("Do you want to proceed?  Y/N  ")
if should_proceed.lower() in ["y", "yes"]:
    # handle affirmative answer here
    print("OK, you got it!")
elif should_proceed.lower() in ["n", "no"]:
    # handle negative answer here
    print("Thank you anyways, {}!".format(name))
else:
    # give "friendly" response otherwise
    print("I did not understand that answer.")
Joel Williams
Joel Williams
800 Points

Thank you! I was just getting a little ahead of myself and didn't fully understand lists or the syntax used. When I tested it I used 'and' instead of a comma and it was giving me strange results.