0

I am starting out, so this code may not be as efficient as it could be, but I try. This is a text based game, something happens and the player must decide if he wants to continue or not. but Everyone is different. so everyone is going to answer with some variation of "yes". How can I get the program to move on if someone types any variation of "yes" the commas give me a syntax error, and i don't know how else to get it to separate the possible answers.

def continueyn():

    option1_des1 = raw_input("> ")

    if option1_des1 == "yes", "Yes", "forward", "Forward", "Full impulse", "Full Impulse", "yeah", "yup", "Yup", "Yeah":
        gravityp1()
    elif option1_des1 == "no":
        bearing(), userinput()
    else:
        print "Sorry Captain, I didn't get that. What did you say?"
        continueyn()
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – jonrsharpe Jul 15 '14 at 14:17
  • 1
    Also, note that e.g. [`str.lower()`](https://docs.python.org/2/library/stdtypes.html#str.lower) will allow you to ignore case in the comparison - just make sure the strings you're comparing to are all lower case. – jonrsharpe Jul 15 '14 at 14:18
  • `if option1_des1 in ("yes", "Yes", "forward", "Forward", "Full impulse", "Full Impulse", "yeah", "yup", "Yup", "Yeah"):` Should work for you. – Raghav RV Jul 15 '14 at 17:08

2 Answers2

0

Using 'or', as pointed out in the comments is incorrect. The correct way to go ahead through this would be either using lists and a loop, or iterating using "in":

Method 1:

option1_des1 = raw_input("> ")
if option1_des1 in {"yes", "Yes", "forward"} :
    print "yes"
elif option1_des1 == "no":
    print "no"
else:
    print "Sorry Captain, I didn't get that. What did you say?"

Method 2: You can also make a list and then go across values of the list like this-

flag=True
yesList = ['yes', 'Yes', 'forward']
option1_des1 = raw_input("> ")

for element in yesList:
    if option1_des1 == element:
        print "yes"
        flag=False
        break;


if option1_des1 == "no" and flag:
    print "no"
elif flag:
    print "Sorry Captain, I didn't get that. What did you say?"

Even better, you can use regex to match several variations of one type instead of figuring out every permutation. The xample below only covers "yes", but you can make a list of words like in method 2.

import re

pattern = 'yes'
option1_des1 = raw_input("> ")
matchObj = re.search(pattern, option1_des1, re.I)
if matchObj:
    print "yes"    
elif option1_des1 == "no":
    print "no"
else:
    print "Sorry Captain, I didn't get that. What did you say?"
sbhatla
  • 1,040
  • 2
  • 22
  • 34
  • 2
    You should also read the question I have suggested as a duplicate - your first suggestion is a classic Python error, which will result in the `"yes"` being printed irrespective of the user's input. Also, you should use actual booleans (`True` and `False`) for `flag`. – jonrsharpe Jul 15 '14 at 14:31
0

Similar to sbhatla's answer you could use a list and use the built in "in" function which returns as a boolean.

yesoptions = ["yes", "Y", "etc"]
if option_des1 in yesoptions:
    #runs actions for a yes answer
elif option in:
    etcetc
else: 
    etc..
charleschanlee
  • 149
  • 2
  • 5