0

I'm trying to code a Rock, Paper, Scissors game. How do I prevent them from entering random words/letters and make them reinput their choices until they hit one of the viable choice in the Rock, Paper, Scissors game? For example: "Your input is not viable. Please try again: "

import random

def play():

    user = input("Choose your secret weapon! 'Rock' or 'Paper' or 'Scissors': ")
    computer = random.choice(['Rock', 'Paper', 'Scissors'])

    if user == computer:
        return ("It's a tie! You and computer have both chosen {}.").format(user)

    if win(user, computer):
        return("You've won! You chose {} and computer chose {}.").format(user, computer)
    return("You've lost... You chose {} and computer chose {}.").format(user, computer)

def win(user, computer):
    if(user == "Scissors" and computer == "Paper") or (user == "Paper" and computer == "Rock") or (user == "Rock" and computer == "Scissors"):
        return True
    return False

print(play())

mcmong23
  • 9
  • 1

1 Answers1

0

You could use a while loop wich chekcks if it is a valid input something like

user_input = input("Choose your secret weapon! 'Rock' or 'Paper' or 'Scissors': ")
while user_input.lower() not in ['rock', 'paper', 'scissors']:
    user_input = input("Your input is not viable. Please try again: ")
#Your code
Yanni2
  • 176
  • 9