r/learnpython • u/live4hisglory • Feb 28 '14
If statements and strings
Hi there, I'm new to learning the language (and programming in general). I am working my way thought LPTHW and am on ex36, where you are making your own text adventure using if statements.
My question is: Can you use AND/OR statements within an IF statement for strings?
Ex.
if "stringa" or "stringb" in xyz:
or
if "stringa" and "stringb" in xyz:
I did some searching here and on google with no luck.
As it is right now, I've had to imbed IF statements within IF statements to get the AND functionality to work and just use parallel IF statements for OR functionality.
To me this seems sloppy and inefficient, there must be a better way.
I've found AND/OR statements work well with IF statements so long as you are working with BOOL values. My issue is with strings.
4
Upvotes
1
u/ingolemo Mar 01 '14
The way that the expression is grouped together isn't actually that important because neither
"stringa" or ("stringb" in xyz)nor("stringa" or "stringb") in xyzwould do what the op wants. The real problem is that python has no way to know that the op wants it to distribute thein xyzoperation over theoroperator.