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
David Tardy
Python Web Development Techdegree Student 3,270 Pointselif first_name == "bre" or "Bre"
I tried to make this my elif statement because I got the wrong answer when I did not capitalize my input. So I thought I could add the 'Bre or bre' code and it worked for bre both ways, however it provided the same out put when I typed Sam as the name.
the elif worked for Bre, bre as well as Sam. Why wouldnt Sam go to the else: statement?
Heres the code.
first_name = input("What is your first name? ") if first_name == "David": print(first_name, "is learning python") elif first_name == "Bre" or "bre": print(first_name, "is learning Python with fellow students in the community.") else: print("You should totally learn python {}!" .format(first_name)) print("Have a great day {}!".format(first_name))
1 Answer
Steven Parker
243,199 PointsYou can only combine complete comparison expressions using logic operators (such as "or"). And there are also other tests you can use in this situation:
elif first_name == "bre" or "Bre" # incomplete, will always be "truthy"
elif first_name == "bre" or first_name == "Bre" # combining complete expressions works
elif first_name in ["bre", "Bre"] # a more compact way to test for two values
elif first_name.lower() == "bre" # or do a case-insensitive comparison
David Tardy
Python Web Development Techdegree Student 3,270 PointsDavid Tardy
Python Web Development Techdegree Student 3,270 PointsThanks!