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.
Ali AlRabeah909 Points
Dice rolling game. What's wrong with my code?
When prompt it accepts any answer as "yes".
import random def roll_dice(answer): print("You will get a number between 1 and 6.") for numbers in range(1): print(random.randint(1, 6)) a = True while a == True: yes_or_no = input("Would you like to roll dice? Y/N ") if yes_or_no == "Yes" or "Yes".lower(): roll_dice(yes_or_no) else: are_you_sure = input("Are you sure you want to exit? Y/N ") if are_you_sure == "Yes" or "Yes".lower(): print("Goodbye!") break else: continue
Alex KoumparosPython Development Techdegree Student 36,862 Points
You are making a subtle mistake in your comparison:
if yes_or_no == "Yes" or "Yes".lower():
For the rest of the example, let's assume that the user entered
The way that
or works is that it checks two boolean values (one to the left of the
or, one to the right). In this case the boolean value to the left of the
yes_or_no == "Yes".
"no" is not equal to
"Yes" so the left side evaluates to False.
The boolean value to the right of the
"Yes".lower(). In Python, non-empty strings are 'truthy' so this evaluates to
or evaluates to
True if either side is
True, and the right side will always be
if will always be triggered.
One way that you could have rewritten your comparison is to compare
yes_or_no, just like you did on the left side of the
or. Alternatively, you could just make whatever value is in
yes_or_no all lowercase and then you only need to make one comparison (the lowercase version of the value in
Hope that clears everything up