来自Big Bang Theory的Python RPSSL游戏,如果输入!=列出的选项怎么办



我花了一些时间,但我从大爆炸理论中创建了石头、纸、剪刀、斯波克、蜥蜴游戏,现在我不知道我的代码是否以最有效的方式制作,我被告知我可以使用 do while 循环来使其更容易,但这不涉及我的问题。我的问题是,当有人输入输入时,他们可以输入任何他们想要的东西,它不一定是"岩石"纸"剪刀"斯波克"蜥蜴"。有没有办法强迫他们输入R.P.S.S.L,有没有办法强迫用户从以下选项中进行选择。我知道每个"有办法"的问题通常是肯定的,有办法,但我主要在 youtube 上搜索了一些关于如何做到这一点的信息,但在这个网站上搜索了一些,但我可以找到一种方法来做到这一点,因为我听说输入有时很困难,并且较新的 python 更新改变了我们使用输入的方式。提前感谢您,感谢您的时间。

#Main
import random
player_rps = input('Rock, Paper, Scissors, Lizard, or Spock:t').upper()
computer_rps = ['ROCK','PAPER','SCISSORS','SPOCK','LIZARD']
game_rps = random.choice(computer_rps)
#Player
if player_rps == 'ROCK':
print('Player picked Rock')
elif player_rps == 'PAPER':
print('Player picked Paper')
elif player_rps == 'SCISSORS':
print('Player picked Scissors')
elif player_rps == 'SPOCK':
print('Player picked Spock')
elif player_rps == 'LIZARD':
print('Player picked Lizard')
#Computer
if game_rps == 'ROCK':
print('Computer picked Rock')
elif game_rps == 'PAPER':
print('Computer picked Paper')
elif game_rps == 'SCISSORS':
print('Computer picked Scissors')
elif game_rps == 'SPOCK':
print('Computer picked Spock')
elif game_rps == 'LIZARD':
print('Computer picked Lizard')
#Output for rock
if player_rps == "ROCK" and game_rps == "SCISSORS":
print("Rock crushes scissors, the Player wins!")
if player_rps == "SCISSORS" and game_rps == "ROCK":
print("Rock crushes scissors, the Computer wins!")
if player_rps == "ROCK" and game_rps == "PAPER":
print("Paper covers rock, the Computer wins!")
if player_rps == "PAPER" and game_rps == "ROCK":
print("Paper covers rock, the Player wins!")
if player_rps == "ROCK" and game_rps == "SPOCK":
print("Spock vaporizes rock, the Computer wins!")
if player_rps == "SPOCK" and game_rps == "ROCK":
print("Spock vaporizes rock, the Player wins!")
if player_rps == "ROCK" and game_rps == "LIZARD":
print("Rock crushes lizard, the Player wins!")
if player_rps == "LIZARD" and game_rps == "ROCK":
print("Rock crushes lizard, the Computer wins!")
#Output for paper
if player_rps == "PAPER" and game_rps == "LIZARD":
print("Lizard eats paper, the Computer wins!")
if player_rps == "LIZARD" and game_rps == "PAPER":
print("Lizard eats paper, the Player wins!")
if player_rps == "PAPER" and game_rps == "SCISSORS":
print("Scissors cuts paper, the Computer wins!")
if player_rps == "SCISSORS" and game_rps == "PAPER":
print("Scissors cuts paper, the Player wins!")
if player_rps == "PAPER" and game_rps == "SPOCK":
print("Paper disproves spock, the Player wins!")
if player_rps == "SPOCK" and game_rps == "PAPER":
print("Paper disproves spock, the Computer wins!") 
#Output for scissors
if player_rps == "SCISSORS" and game_rps == "SPOCK":
print("Spock smashes scissors, the Computer wins!")
if player_rps == "SPOCK" and game_rps == "SCISSORS":
print("Spock smashes scissors, the Computer wins!")
if player_rps == "SCISSORS" and game_rps == "LIZARD":
print("Scissors decapitates lizard, the Player wins!")
if player_rps == "LIZARD" and game_rps == "SCISSORS":
print("Scissors decapitates lizard, the Computer wins!")
#Output for spock
if player_rps == "SPOCK" and game_rps == "LIZARD":
print("Lizard poisons spock, the Computer wins!")
if player_rps == "LIZARD" and game_rps == "SPOCK":
print("Lizard poisons spock, the Player wins!")
if player_rps == game_rps:
print("It's a tie!")
else:
print("Please enter the correct option: Rock, Paper, Scissors, Spock,             
Lizard")

您可以通过 while 循环来获取玩家输入:

#Main
import random
computer_rps = ['ROCK','PAPER','SCISSORS','SPOCK','LIZARD']
valid_input = False  # Make sure to enter at least once
while valid_input is False:
player_rps = input('Rock, Paper, Scissors, Lizard, or Spock:t').upper()
if player_rps in computer_rps:
valid_input = True
game_rps = random.choice(computer_rps)
#Player
if player_rps == 'ROCK':
print('Player picked Rock')
elif player_rps == 'PAPER':
print('Player picked Paper')
elif player_rps == 'SCISSORS':
print('Player picked Scissors')
elif player_rps == 'SPOCK':
print('Player picked Spock')
elif player_rps == 'LIZARD':
print('Player picked Lizard')
#Computer
if game_rps == 'ROCK':
print('Computer picked Rock')
elif game_rps == 'PAPER':
print('Computer picked Paper')
elif game_rps == 'SCISSORS':
print('Computer picked Scissors')
elif game_rps == 'SPOCK':
print('Computer picked Spock')
elif game_rps == 'LIZARD':
print('Computer picked Lizard')
#Output for rock
if player_rps == "ROCK" and game_rps == "SCISSORS":
print("Rock crushes scissors, the Player wins!")
if player_rps == "SCISSORS" and game_rps == "ROCK":
print("Rock crushes scissors, the Computer wins!")
if player_rps == "ROCK" and game_rps == "PAPER":
print("Paper covers rock, the Computer wins!")
if player_rps == "PAPER" and game_rps == "ROCK":
print("Paper covers rock, the Player wins!")
if player_rps == "ROCK" and game_rps == "SPOCK":
print("Spock vaporizes rock, the Computer wins!")
if player_rps == "SPOCK" and game_rps == "ROCK":
print("Spock vaporizes rock, the Player wins!")
if player_rps == "ROCK" and game_rps == "LIZARD":
print("Rock crushes lizard, the Player wins!")
if player_rps == "LIZARD" and game_rps == "ROCK":
print("Rock crushes lizard, the Computer wins!")
#Output for paper
if player_rps == "PAPER" and game_rps == "LIZARD":
print("Lizard eats paper, the Computer wins!")
if player_rps == "LIZARD" and game_rps == "PAPER":
print("Lizard eats paper, the Player wins!")
if player_rps == "PAPER" and game_rps == "SCISSORS":
print("Scissors cuts paper, the Computer wins!")
if player_rps == "SCISSORS" and game_rps == "PAPER":
print("Scissors cuts paper, the Player wins!")
if player_rps == "PAPER" and game_rps == "SPOCK":
print("Paper disproves spock, the Player wins!")
if player_rps == "SPOCK" and game_rps == "PAPER":
print("Paper disproves spock, the Computer wins!") 
#Output for scissors
if player_rps == "SCISSORS" and game_rps == "SPOCK":
print("Spock smashes scissors, the Computer wins!")
if player_rps == "SPOCK" and game_rps == "SCISSORS":
print("Spock smashes scissors, the Computer wins!")
if player_rps == "SCISSORS" and game_rps == "LIZARD":
print("Scissors decapitates lizard, the Player wins!")
if player_rps == "LIZARD" and game_rps == "SCISSORS":
print("Scissors decapitates lizard, the Computer wins!")
#Output for spock
if player_rps == "SPOCK" and game_rps == "LIZARD":
print("Lizard poisons spock, the Computer wins!")
if player_rps == "LIZARD" and game_rps == "SPOCK":
print("Lizard poisons spock, the Player wins!")
if player_rps == game_rps:
print("It's a tie!")

它会一直持续下去,直到您获得有效的输入。

相关内容

最新更新