将输入验证添加到我的Rock Paper Scissor PYTHON

时间:2014-10-22 00:38:12

标签: python validation input

我正在尝试为此添加输入验证,因此用户只能输入ROCK,rock,PAPER,paper,SCISSORS或剪刀。我不确定在哪里添加它,以及如何将其作为if语句。 任何帮助是极大的赞赏。当然,我需要在他们纠正答案后完成比赛

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()

3 个答案:

答案 0 :(得分:2)

guess = raw_input('rock, paper or scissors?: ')之后,您可以通过简单的方式进行检查:

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

注1: 而不是处理'ROCK'和'摇滚'等,我建议你将用户输入转换为大写,并只处理大写字符串:

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

注2: 我建议您列出所有可能的答案:

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

然后就这样做:

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

答案 1 :(得分:0)

如果我运行并输入CATS,它会说"输入石头纸或剪刀"喜欢它应该,但它也说它是一个领带,这是错误的。因此,在输入错误之后,它应该基本上回到用户输入

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()

答案 2 :(得分:0)

那是因为如果它不是一个有效的选择,那么你需要再次提出问题,而不是继续处理。你需要一个循环,例如:

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)