0

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()
2
  • Is stackoverflow.com/questions/26456215/… of any help
    – Parker
    Oct 22, 2014 at 0:41
  • You might want to look into the lower() method of python strings. This will simplify your code, e.g. 'rOcK'.lower() == 'rock'
    – Dan
    Oct 22, 2014 at 0:45

3 Answers 3

2

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.
1
  • Thank you, that helps. Once I run it though, i get this in the editor rock, paper or scissors?: cat Please enter rock, paper or scissors: ('CPU: ', 'ROCK', 'Player: ', 'cat') Its a tie try again! rock, paper or scissors?: Then it says its a tie, to try again. how can i write it so it doesnt? Ill add my code as next answer Oct 22, 2014 at 13:18
0

If I run this and type CATS, it will say "Enter rock paper or scissors" like its supposed to, but it also says its a tie, which is.. wrong. So, after a wrong input, it should basically go back to user input

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?: ')
    if guess not in ('ROCK', 'rock', 'PAPER', 'paper', 'SCISSORS', 'scissors'):
        print"Please enter rock, paper or scissors:"


    result = winner(x, guess)
    print('CPU: ', x, 'Player: ', 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()
1
  • that's because if it's not a valid choice, then you need to ask the question again instead of moving on to processing. You need to have a loop, see my other answer.
    – Gui Rava
    Oct 22, 2014 at 17:19
0

that's because if it's not a valid choice, then you need to ask the question again instead of moving on to processing. You need to have a loop, for instance:

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

while True:
    guess = raw_input('rock, paper or scissors?: ')
    guess = guess.upper()
    if guess in valid_choices : break

result = winner(x, guess)
print('CPU: ', x, 'Player: ', guess)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.