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 trialTor Ivan Boine
4,057 PointsWhy can't I use OR in if statement?
I found out that I have to .lower the input. But I first tried with 'n' or 'N'. And that didn't work. Seemed logical that it would. Why doesn't it work?
import sys
start = input('want to start the movie? ')
if start != 'n' or 'N':
print ('Enjoy the show!')
else:
sys.exit()
2 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHi there,
You're doing really well so far, there are just a couple of things.
First, when checking multiple conditions on the same line, everything needs to be repeated for the subsequent conditions. Instead of:
if start != "n" or "N":
It needs to be:
if start != "n" or start != "N":
Make sense? Each condition needs to be told again what it is comparing to.
Second, you can't use or
in this example. With or
, only one of the conditions has to evaluate to True
to pass. So, the first check is looking for not equal
to a lower case 'n'... if someone puts in an upper-cased "N" then this returns True as it is Not Equal to the lower case and will trigger the if block
, and the second condition will not even run.
With and
, both must return true to execute. So, an upper-cased "N" passes the first condition, but gets 'caught' on the second so the whole line returns False
and the program executes the else
block.
I hope that makes sense!
Other wise, nice work. Just one more tip. in python, it is the convention to not have a space separating the method and the parameters. So, print ('Enjoy the show!')
should be print('Enjoy the show!')
. The former won't cause an error, but it should be the latter.
Keep Coding! :)
Tor Ivan Boine
4,057 Pointsah ofc. that makes sense :)
those not not true and or false are still messing with my brain. Will need to spend some more times with the not not true bools :p
Thank you on the space "issue". Just a habit that I need to relearn.
Jason Anders
Treehouse Moderator 145,860 PointsThe "not not true" things still mess with my brain too, so believe me ... you're not alone on that one. Lol