-2

If the user does not select 1 or 2, I want the code to say "Please enter 1 or 2 to continue", that part works. However if user inputs "6" it asks "Please enter 1 or 2 to continue" as it should but if a valid input is entered directly after an invalid input, code does not display correctly.

I've tried to do this without the requirement function but nothing seems to work how I want it to.

def requirement():
    choice = ""
    while choice != "1" and choice != "2":
        choice = input ("Please enter 1 or 2 to continue.\n")
    if choice == "1" and choice == "2":
        return choice

def intro():
    print ("Enter 1 to enter the cave\n")
    print ("Enter 2 to explore the river\n")

    play_again = input ("What would you like to do?\n")
    if play_again in "1":
        print ("You win!")
    elif play_again in "2":
        print ("YOU LOSE")
        print ("Thanks for playing!")
        exit()
    else:
        requirement()
intro()
  • 1
    `if choice == "1" and choice == "2":` `choice` will never be equal to `1` and `2` at the same time. – Reblochon Masque Jul 10 '19 at 15:55
  • Either way, when `requirement()` exits, then `intro()` will exit. End of program. – quamrana Jul 10 '19 at 15:57
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Jul 10 '19 at 15:58

1 Answers1

0
def intro():
    print ("Enter 1 to enter the cave\n")
    print ("Enter 2 to explore the river\n")
    play_again = input ("What would you like to do?\n")
    return play_again

def game(choice):
    if choice == "1":
        print ("You win!")
    elif choice == "2":
        print ("YOU LOSE")
        print ("Thanks for playing!")
        exit()
    else:
        choice = input ("Please enter 1 or 2 to continue.\n")
        game(choice)

game(intro())

The else statement already takes care of whether or not a 1 or 2 is input, so there is no need for the requirement function.