我想添加輸入驗證,以便用戶只能輸入岩石,岩石,紙張,紙張,剪刀或剪刀。我不確定在哪裏添加它,以及如何將它作爲if語句來執行。 任何幫助,不勝感激。當然,我需要完成比賽guess = raw_input('rock, paper or scissors?: ')
後後,他們修正他們的答案添加輸入驗證到我的岩石剪刀PYTHON
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()
是http://stackoverflow.com/questions/26456215/python-3-rock-paper-scissors-issue/26456305任何幫助 – Parker 2014-10-22 00:41:02
您可能要查看python字符串的'lower()'方法。這會簡化你的代碼,例如''rOcK'.lower()=='rock'' – Dan 2014-10-22 00:45:27