tags:

views:

149

answers:

6

I've tried Google, but I can't find the answer to this simple question. I hate myself for not being able to figure this out, but here we go.

How do I write an if statement with or in it?

For example:

if raw_input=="dog" or "cat" or "small bird":
    print "You can have this animal in your house"
else:
    print "I'm afraid you can't have this animal in your house."
+16  A: 

You can put the allowed animals into a tuple then use in to search for a match

if raw_input() in ("dog", "cat", "small bird"):
    print "You can have this animal in your house"
else:
    print "I'm afraid you can't have this animal in your house."

You can also use a set here, but I doubt it would improve performance for such a small number of allowed animals

desired_animal = raw_input()
allowed_animals = set(("dog", "cat", "small bird"))
if desired_animal in allowed_animals:
    print "You can have this animal in your house"
else:
    print "I'm afraid you can't have this animal in your house."
gnibbler
+1 Most pythonic solution presented, could be made more functional, though.
marr75
@marr It was a pretty trivial question, I don't think we need to start refactoring it to make it more "functional"
Michael Mrozek
@marr75, please feel free to post a more functional answer :)
gnibbler
@Michael I didn't mean it needed to be, it just seems like "pythonic" is starting to have more functional programming connotations these days, could be just my opinion. @gnibbler the only functional code I can think of (map and then reduce) would be harder to read, probably.
marr75
+1  A: 
if (raw_input=="dog") or (raw_input == "cat") or (raw_input == "small bird"):
  print You can have this animal in your house
else:
  print I'm afraid you can't have this animal in your house.

or

if raw_input in ("dog", "cat", "small bird"):
  print You can have this animal in your house
else:
  print I'm afraid you can't have this animal in your house.
deinst
Not very "pythonic", i've written code like this but I wouldn't recommend it to a new programmer.
marr75
assuming the raw_input is supposed to be the function call raw_input() you would not want to call it 3 times
gnibbler
A: 
if (raw_input=="dog") or (raw_input=="cat") or (raw_input=="small bird"):
    print You can have this animal in your house
else:
    print "I'm afraid you can't have this animal in your house."
Matt Ball
assuming the `raw_input` is supposed to be the function call `raw_input()` you would not want to call it 3 times
gnibbler
+10  A: 

If you want to use or, you need to repeat the whole expression each time:

if raw_input == "dog" or raw_input == "cat" or raw_input == "small bird":

But a better way to do this particular comparison is with in:

if raw_input in ("dog", "cat", "small bird"):
Daniel Roseman
A: 

You can do it this way

 if raw_input=="dog" or raw_input=="cat" or raw_input=="small bird":
Lou Franco
A: 
goodanimals= ("dog" ,"cat","small bird")
print("You can have this animal in your house" if raw_input().strip().lower() in goodanimals
      else "I'm afraid you can't have this animal in your house.")
Tony Veijalainen