Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
229,695 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.