Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,086 Points

My try block is always triggering a ValueError, and I need help finding out why.

Hi everyone!

The code below is designed to catch input errors.

When the player is asked if they'd like to play again, I want them to see an error message upon entering anything other than "yes" or "no".

Here's the problem: any input is triggering the error message- even "yes" or "no"!

                input_error = True
                while input_error:
                    try:
                        valid_input_1 = "yes"
                        valid_input_2 = "no"
                        option_to_play_again = input("\nWould you like to play again? (Yes/No) ")
                        if option_to_play_again.lower() != valid_input_1:
                            raise ValueError("Oh no! We ran into an issue. Please enter only yes or no.")
                        elif valid_input_2 != option_to_play_again.lower():
                            raise ValueError("Oh no! We ran into an issue. Please enter only yes or no.")
                    except ValueError as err:
                        print(f'{err}')
                    else:
                        input_error = False

I am having trouble determining what the issue is. Can anyone help me? Any insight would be appreciated.

Thanks for reading!

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 33,545 Points

Hi Asher. So your issue is in your if and elif. Your first IF checks if answer is not equal to yes. So if it equals yes then it’s false and moves to the else. However no is not equal to yes so no will give an error. Then similar on the second if. If it’s yes then it gets to the second if but now it’s not equal to no so it gives an error. To help see this change your error messages to be different and you will see which gets triggered when. To fix you want to check if answer is not equal to yes AND answer is not equal to no. So if it equals yes or no one side of the AND will be false and not trigger an error. So something like

If answer != ‘yes’ and answer != ‘no’: Raise error

Good luck and If you still have trouble be sure to post back to the community.

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,086 Points

Thank you, Mark! Your comment was very helpful. I see now that the if/elif statements inside my try block could never register False, which kept me trapped in the while input_error loop.

I changed my code to this:

while input_error:
                    try:
                        valid_input_1 = "yes"
                        valid_input_2 = "no"
                        option_to_play_again = input("\nWould you like to play again? (Yes/No) ")
                        if option_to_play_again.lower() != valid_input_1 and option_to_play_again.lower() != valid_input_2:
                            raise ValueError("Oh no! We ran into an issue. Please enter only yes or no.")
                    except ValueError as err:
                        print(f'{err}')
                    else:
                        input_error = False

... which solved the problem. Thanks again!

Mark Sebeck
Mark Sebeck
Treehouse Moderator 33,545 Points

You’re welcome Asher. Happy to help and thanks for letting me know you solved it.