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
Benjamin Hedgepeth
5,672 PointsUsing 'or' within a conditional statement
Please refer to Line 4 of my code that begins with the if statement. How can I make the block execute properly when the condition is not met? For any 'choice' I make I always get "Do you want whipped cream for $0.25 extra?"
shakes = ['Vanilla', 'Strawberry', 'Double Chocolate', 'Marshmallow', 'Mint']
for flavor in shakes:
choice = input("Do you want a small, medium, or large size for your {} shake? ".format(flavor))
if choice != "Medium" or "Large":
print("Do you want whipped cream for $0.25 extra?")
else:
print("You get complementary whipped cream with your purchase!")
1 Answer
Hannu Shemeikka
16,799 PointsHi,
The problem is that on line
if choice != "Medium" or "Large":
you are comparing if choice is not "Medium" 'or' "Large" is true. You are not comparing if choice is not "Large". Since the second 'or' comparison is always true, "Do you want whipped cream for $0.25 extra?" is always printed.
You always need to include the variable when you are comparing it to something, e.q.
if choice != "Medium" or choice != "Large"
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsAlso, I think
orneeds to be changed toandfor the logic to work out. In other words, the choice can't be medium and it can't be large.I'm assuming that whipped cream is extra for small size only.
Benjamin,
Probably the closest you can get to what you were trying to do is:
if choice not in ["medium", "large"]:That would be logically equivalent to what Hannu wrote IF you change the
ortoandIf it's true that cream is extra for small size only then you could take what I would consider a more direct approach on the logic.
Only check if the choice equals "small"
if choice == "small":