1

fairly new to python, and coding in general. I am following an online guide for a simple Python function, but I want to edit it so I can learn more it. I want to know how my function can look for a variety of inputs via the user that will activate the same output. For example, I want 'Yes' and 'yes' to output the same text.

choice = raw_input('Do you like YouTube videos?  ')
if choice == 'yes' :
    print 'I like YouTube videos too!'
elif choice == 'no' :
    print 'Damn, you suck'
else :
    print '*invalid answer*'
marmeladze
  • 6,468
  • 3
  • 24
  • 45
Jake Jackson
  • 1,055
  • 1
  • 12
  • 34
  • 2
    the classic way is to use a set : `if choice.lower() in {'yes', 'y','yup'}` (`.lower()` just avoids to write all caps versions) – PRMoureu Nov 09 '17 at 22:33
  • Possible duplicate of [How do I test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-do-i-test-multiple-variables-against-a-value) – PRMoureu Nov 09 '17 at 22:38

3 Answers3

0

Simplest version:

choice = raw_input('Do you like YouTube videos?  ')
if choice == 'yes'  or choice == 'Yes':
    print 'I like YouTube videos too!'
elif choice == 'no' :
    print 'Damn, you suck'
else :
    print '*invalid answer*'

However you can compare user output without taking of case sensivity:

if choice.lower() == 'yes':
domandinho
  • 1,260
  • 2
  • 16
  • 29
0

This code will explain you and will cover all test cases

//Asks for user input    
choice = raw_input('Do you like YouTube videos?  ')
// Convert Input to lower case or upper 
choice = choice.lower()
//so Now you have lower all the character of user input you just need to compare it with lower letter 
if choice == 'yes' :
print 'I like YouTube videos too!
elif choice == 'no' : 
print 'Damn, you suck'
else : 
print 'invalid answer'
Mike
  • 512
  • 3
  • 16
0

In addition to lower casing the letters, you may want to also consider using strip() to remove extra spaces from the begining and end of your string. For example:

answer = "Yes "

if answer == "Yes":
    print("Pass")
else:
    print("Fail!")
// output = Fail!

However,

answer = "Yes "

if answer.strip() == "Yes":
    print("Pass")
else:
    print("Fail!")
// output = pass

As a result and generally speaking, lower casing, using strip(), and all of these similar operations are a kind of pre-processing step and cleaning that you may need to do based on the specific task on which you are working. Just wanted to give you the big picture here!

Pedram
  • 2,421
  • 4
  • 31
  • 49