Adding input validation to my Rock Paper Scissor PYTHON
Question: I am trying to add input validation to this so the user can only enter ROCK,
rock, PAPER, paper, SCISSORS or scissors. I am unsure about where to add it,
and really how to do it as an if statement. Any help is greatly appreciated.
Of course I would need to finish the game after they corrected their answer

    
    
    import random
    
    
    def main():
        x = random.randint(1, 3)
        rock = "ROCK, rock"
        paper = "PAPER, paper"
        scissors = "SCISSORS, scissors"
        if x == 1:
            x = 'ROCK'
        elif x == 2:
            x = 'PAPER'
        elif x == 3:
            x = 'SCISSORS'
        guess = raw_input('rock, paper or scissors?: ')
        print('CPU: ', x, 'Player: ', guess)
        result = winner(x, guess)
        if result == 'tie':
            print('Its a tie try again!')
            main()
        else:
            print(result, 'Wins')
    
    def winner(x, guess):
        if guess == 'scissors' and x == 'ROCK':
            win = 'rock'
            return win
        elif guess == 'paper' and x == 'SCISSORS':
            win = 'scissors'
            return win
        elif guess == 'paper' and x == 'ROCK':
            win = 'paper'
            return win
        elif guess == 'rock' and x == 'PAPER':
            win = 'paper'
            return win
        elif guess == 'rock' and x == 'SCISSORS':
            win = 'rock'
            return win
        else:
            win = 'tie'
            return win
    if __name__ == '__main__':
        main()
    


Answer: right after `guess = raw_input('rock, paper or scissors?: ')` , you could
check with a simple:

    
    
    if guess not in ('ROCK', 'rock', 'PAPER', 'paper', 'SCISSORS', 'scissors') : ...
    

Note 1: Instead of handling 'ROCK' and 'rock' , etc. I suggest you convert
user input to uppercase, and only handle upper case strings :

    
    
    guess = raw_input('rock, paper or scissors?: ')
    guess = guess.upper()
    

Note 2: I suggest you have a list of all possible answers :

    
    
    valid_choices = ('ROCK','PAPER','SCISSORS')
    

and then just do:

    
    
    guess = raw_input('rock, paper or scissors?: ')
    guess = guess.upper()
    if guess not in valid_choices : ... #handle error here.
    

