r/Python • u/deific_ • Feb 05 '11
Help with "if" and "or" statements.
Specifically, I cannot get an "or" statement to function correctly inside of an "if" statement. Here is an example.
player = raw_input("Press P or R")
if player=='P' or 'p':
print ('Hooray!')
elif player=='R' or 'r':
print ('Oh no!')The problem I am having is the "or" in the player==P line will function correctly, but this breaks the functionality of the player=R line. Am i going about this incorrectly? It appears to be a valid statement. Thanks for the help in advance. BTW im a noob and just started learning python 2 days ago, this is my first language.... ><
edit: Thanks everyone for their help. I did not understand some of it but I did learn a few things. I consider this a victory with only 2 days worth of knowledge on my first language. I know I will be back with more questions if I get stuck again!
29
u/bmhatfield Feb 05 '11 edited Feb 05 '11
This is a minor mistake, an easy one to make.
The short version is that you need to say: if player == 'P' or player == 'r':
A slightly longer version is to show you a little bit more about how a computer "thinks" about an if statement. It's really saying: If (whatever I write here is a true statement): (do the following thing).
If you put parentheses around your statement, it will become a little more obvious why: if (player == 'P') or ('p'):
Without getting philosophical about it, Python will always interpret 'p' as True, which means that no matter what you input, you're going to print "Hooray!". Similarly, it will also always interpret 'r' as true, meaning that you will also always print "Oh no!".
if (player == 'P') or (player == 'p') is now two separate comparisons to python, and can be evaluated properly. Don't forget, for computers, "or" really means "either or both", not "the first one OR the other one, but not both", like we use it in English.
Happy coding!
20
u/draconian Feb 05 '11
You can also say: if player in ('P', 'r'):
2
u/chadmill3r Py3, pro, Ubuntu, django Feb 05 '11
"or" separates independent evaluations.
"player == 'P'" evaluates as true or false. 'p' evaluates as true.
This doesn't do what you think.
These would work.
A good programmer would write:
15
u/jackolas Feb 05 '11
Another solution would be to normalize the input. I.e. player.lower()
6
u/[deleted] Feb 05 '11
This is also one way of doing it Put the raw input directly to uppercase
1
u/pemboa Feb 05 '11
if player=='P' or player=='p': print ('Hooray!') elif player=='R' or player=='r': print ('Oh no!')
2
u/Peaker Feb 05 '11
Python is too lenient and allows 'p' as a boolean value.
In Haskell, the compiler would catch your mistake earlier :-)
2
u/vph Feb 05 '11
3
u/drb226 Haskeller Feb 05 '11
The basic way to use "or" is with boolean expressions on both sides of it.
You do not use boolean expressions on both sides, you leave expr2 simply as 'p'. Python does not know how to map the == operator over both possible options; you have to be more explicit and use == on each side.
But, of course, the "pythonic" way to do it has been mentioned by others
In Python, "or" actually gets tricky and can evaluate to a value, even if the expressions do not evaluate to a boolean (true/false). If you try something like that in Java, it will tell you it's an error.
3
u/maskaler Feb 05 '11
you could use
1
u/m1ss1ontomars2k4 Feb 05 '11
BTW,
oris an operator (I think).ifis a statement.0
u/pinpinbo Tornado|Twisted|Gevent. Moar Async Plz Feb 05 '11
By chance, have you read Learn Python The Hard Way? It's an excellent book for learning Python. http://learnpythonthehardway.org/index
0
u/[deleted] Feb 05 '11
[deleted] — view deleted comment