0

im new to StackOverflow and Python, I am working on my first program that is not hello world, it is a simple rock, paper, scissors game.

So, my question is can you ask multiple things in an if statement? quick sample

sport = raw_input("Whats a good sport?")
if sport == 'Football','Soccer','Hockey':
   print 'Those are fun sports!'
elif sport != 'Football','Soccer','Hockey':
print 'I dont like those sports!'

I know there are ways to fix that code but I am curious as to if that is a thing?

Manerd
  • 41
  • 1
  • 3
  • 2
    Lemme find the duplicate(s) .. :} – user2864740 Jul 08 '15 at 01:41
  • what about the logical operator or **||**? –  Jul 08 '15 at 01:41
  • 1
    http://stackoverflow.com/questions/17615020/what-is-the-best-approach-in-python-multiple-or-or-in-in-if-statement , http://stackoverflow.com/q/26891304/2864740 , http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values , http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true?lq=1 – user2864740 Jul 08 '15 at 01:41

2 Answers2

3

You can use and and or:

if sport == "Fooball" or sport == "Soccer":
    print "those are fun sports!"

You can also check for a string in a list (or in the following example, a tuple) of strings:

if sport in ("Football", "Soccer", "Hockey"):
    print "those are fun sports"
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you for the hasty response! Sorry for my noobiness in python – Manerd Jul 08 '15 at 01:42
  • Isn't that technically a tuple and not a list of strings? – nalyd88 Jul 08 '15 at 02:38
  • @nalyd88: technically, yes it is a tuple. Conceptually it is a list of items. Considering that the person who asked the question knows absolutely nothing about programming, I didn't want to get into the differences. I'll update my answer. Thanks for the feedback. – Bryan Oakley Jul 08 '15 at 10:49
0

You could also use regular expressions to find if the sport is fun or not. The benefit to regular expressions is that it makes it easy to handle different scenarios. For example if the user inputted football instead of Football you could still determine that the sport is fun or not.

import re
pattern = re.compile('[fF]ootball|[sS]occer|[hH]ockey')
sport = 'Football'
if pattern.search("Football"):
    print sport + " is fun"
else:
    print sport + " is not fun" 

For more info: https://docs.python.org/3.5/howto/regex.htmlYou