0

I am trying to get this code to take you back to the 'Yes or No?' input if the user types an invalid input. Can you help me? Here is my code so far:

def print_options():
print("~DICE GAME~")
print(" 'p' Print this menu again")
print(" '1' Play")
print(" '2' Instructions")
print(" '3' Quit Game")

choice = "p"
while choice != "3":
    if choice == "1":
        print("****Welcome to the Matrix Game****")
        print("Enter the names for players 1 and 2")
        player1=input("Enter the name of Player 1:")
        player2=input("Enter the name of Player 2:")
        print("Welcome",player1, "and",player2, ", are you ready to conquer 
        the matrix?")
        response=input("Yes or No?")
        if response=="yes" or "Yes" or "yES":
            loadstartgame(player1, player2)
        elif response=="no" or "No" or "nO"?:
            sys.exit()
        else:
            print("invalid choice")
            #here is where i want to go back to the yes or no question
vaultah
  • 44,105
  • 12
  • 114
  • 143
ACE
  • 1
  • you can put it into a while loop like you did with `choice` – neuhaus Aug 30 '17 at 15:30
  • 1
    The feature you're asking for is called `GOTO` in other languages and has been largely removed from modern programming. It caused many problems, which could always be solved with a different idiom that was actually clearer – bendl Aug 30 '17 at 15:32
  • 1
    FYI `response=="yes" or "Yes" or "yES"` does not do what you think it does - it will always evaluate to `True`. You can add brackets to see what is happening => `(response=="yes") or ("Yes") or ("yES")`. To chain you would need to do `response=="yes" or response=="Yes"...` – jambrothers Aug 30 '17 at 16:08

3 Answers3

3

You can achieve that by enclosing this code with a while loop, and also you can change the if conditionals to something more elegant and readable, for example:

   while True:
       response=input("Yes or No?")
           if response.lower() == 'yes':
               loadstartgame(player1, player2)
                break
            elif response.lower() == 'no':
                sys.exit()
            print("invalid choice")

the lower() method will change the string to lower case. You also don't need the last else statement.

Update: I edited my original answer to be more concrete, thanks for the comments. This won't accept any other answer besides variations of yes / no.

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • You can simplify `response=="yes" or "Yes" or "yES"` to `response.lower() == 'yes'`; the same for `'no'`. – Zach Gates Aug 30 '17 at 15:32
  • I always prefer to use object methods like `startswith()` rather than `==` sign. It is more readable IMO – Chen A. Aug 30 '17 at 15:36
  • `startswith` will fail for words like Yemen, yesterday, and any other word that starts with y. – Zach Gates Aug 30 '17 at 15:39
  • 4
    @Vinny I'm not sure I agree with that. `if response == 'yes'` is about as readable as you can get, and doesn't have the problem of matching `'yolo'` or something weird. – bendl Aug 30 '17 at 15:40
  • @ZachGates I'm sure when someone asked for yes / no question, if they answer Yemen they must be meaning yes :) – Chen A. Aug 30 '17 at 15:50
  • @Vinny Normally I'd agree, but we're talking about end users here, and they're in a class of stupid all to themselves. I'm sure there's someone out there who's put Yemen as a response to a yes/no question and been thoroughly confused when the software assumed they meant yes ;) – bendl Aug 30 '17 at 16:06
1

There is no concept like goto of other languages present in Python. *

What you'll want to do is use a nested loop wrapped around the part of the code where you input an option.

response = input("Yes or No?") 
while response.lower() not in ["yes","no"]: #this covers the cases of different upper case letters in the response
    response = input('Please enter either "yes" or "no".')

if response.lower()=="yes":
    loadstartgame(player1, player2)
else:
    sys.exit()   

*goto is generally considered an abusable feature of a language, and that is a long ongoing argument that you can find all over the internet.

RageCage
  • 722
  • 2
  • 6
  • 19
  • People sniped me cause I typed too slow, but I decided to answer anyway. If moderators want to delete answer I will understand. – RageCage Aug 30 '17 at 15:37
  • 1
    Your code will exit on the first iteration if the response is different than yes – Chen A. Aug 30 '17 at 15:38
  • I don't see how? I just tested it – RageCage Aug 30 '17 at 15:41
  • Your while loop tests if the response is not yes or no. then it asks for input, and check if it equals to 'yes'. if not -> sys.exit()' – Chen A. Aug 30 '17 at 15:48
  • Notice the tabbing. The if statement is outside of the while loop. I've added a line break to make that more clear. – RageCage Aug 30 '17 at 16:59
-1

Use another while loop.

        response = 0;
        while response == 0:
            response=input("Yes or No?")
            if response=="yes" or "Yes" or "yES":
                loadstartgame(player1, player2)
            elif response=="no" or "No" or "nO"?:
                sys.exit()
            else:
                print("invalid choice")
                response = 0
Treyten Carey
  • 641
  • 7
  • 17