1

I would like to check for valid input when the user inputs a choice between 'r', 'p', 's' How would I go about doing this? And where in my code do I put it?

I've already tried adding this in the beginning and it didn't work:

possible_choices = ["r", "p", "s"]

def play():
    user = input(
        "What's your choice? 'r' for rock, 'p' for paper, 's' for scissors: "
    ).lower
    computer = random.choice(["r", "p", "s"])
    if user not in possible_choices:
        print('Uh oh! Please select a valid option', play())

entire code:

import random

game_loop = True


def play():
    user = input(
        "What's your choice? 'r' for rock, 'p' for paper, 's' for scissors: "
    ).lower
    computer = random.choice(["r", "p", "s"])

    if user == computer:
        return f"Tie! Computer chose {computer}"

    # r > s, s > p, p > r
    if win_or_lose(user, computer):
        return f"You won! Computer chose {computer}"
    return f"You lost! Computer chose {computer}"


def win_or_lose(player, opponent):

    if (
        (player == "r" and opponent == "s")
        or (player == "s" and opponent == "p")
        or (player == "p" and opponent == "r")
    ):
        return True


def play_again():
    p_a = input("Play again? (y/n): ")
    if p_a == "y":
        print(play())
    elif p_a == "n":
        quit()
    else:
        print("oof! Please choose correct option: (y/n)")
        play_again()


print(play())

while game_loop == True:
    play_again()

coder9741
  • 81
  • 10
  • `print('Uh oh! Please select a valid option', play())` Explain, step by step, how you expect this to work. – Karl Knechtel Dec 12 '20 at 02:25
  • Because shouldn't it check the if statement and if user input isn't in possible_choices then prints 'Uh Oh! and goes back to the beginning of the function. – coder9741 Dec 12 '20 at 02:31
  • Does this answer your question? [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) – kaya3 Dec 12 '20 at 03:46

2 Answers2

1

I recommend a while loop, implemented like this:

def play():
    choices = ['r','p','s']
    while True:
        user = input(
            "What's your choice? 'r' for rock, 'p' for paper, 's' for scissors: "
        ).lower()
        if user in choices:
            break
        else:
            print('choose again')
            continue
...

This structure checks if the input is in the list of choices, and if it is not the user tries again. Also the parenthesis on the .lower() were added.

Some more information on this type of thing can be found here

  • Thank you so much. Can you explain why I need a while loop instead of a simple if statement? – coder9741 Dec 12 '20 at 02:41
  • Seems right - you can make this a little cleaner by directly returning the result as it's a function! @coder9741 using a loop allows you to keep re-running a block of code until an acceptable result is found (consider also adding a way to quit). – ti7 Dec 12 '20 at 02:42
  • @ti7 When I do that it says the continue is unreachable code. – coder9741 Dec 12 '20 at 02:55
  • @ti7 is correct. The while loop continues forever until the conditions for the break statement are met. Just in case you need it, here is a good question on how to do things if the user inputs nothing. [Here](https://stackoverflow.com/questions/22402548/default-values-on-empty-user-input) – Jon Parsons Dec 12 '20 at 05:38
0

Answer:



def play():
    choices = ["r", "p", "s"]
    computer = random.choice(["r", "p", "s"])
    while True:
        user = input(
            "What's your choice? 'r' for rock, 'p' for paper, 's' for scissors: "
        )
        if user in choices:
            break
        else:
            print("Please choose a correct choice.")
            continue

    if user == computer:
        return f"Tie! Computer chose {computer}"

    # r > s, s > p, p > r
    if win_or_lose(user, computer):
        return f"You won! Computer chose {computer}"
    return f"You lost! Computer chose {computer}"


def win_or_lose(player, opponent):

    if (
        (player == "r" and opponent == "s")
        or (player == "s" and opponent == "p")
        or (player == "p" and opponent == "r")
    ):
        return True


def play_again():
    while True:
        p_a = input("Play again? (y/n): ")
        if p_a == "y":
            print(play())
        elif p_a == "n":
            quit()
        else:
            print("Please choose correct option..")
        play_again()


print(play())

while True:
    play_again()
coder9741
  • 81
  • 10