我想用Python写一个石头剪子练习。游戏的核心功能就在那里。但是,我试图为用户输入“石头”、“纸”或“剪刀”以外的内容时编写输出。从下面的代码可以看出,如果用户输入了三个预期答案中的一个以外的内容,则游戏结束时的else语句应该会捕获。如果他们这样做了,他们应该得到“出了问题!”作为输出。
程序代码如下:
print ("Rock...")
print ("Paper...")
print ("Scissors")
player_one = input("Player One, make your move! ")
player_two = input("Player two, make your move! ")
if player_one == "rock" and player_two == "paper":
print ("Player Two Wins!")
elif player_one == "rock" and player_two == "scissors":
print ("Player One Wins!")
elif player_one == "paper" and player_two == "rock":
print ("Player One Wins!")
elif player_one == "paper" and player_two == "scissors":
print ("Player Two Wins!")
elif player_one == "scissors" and player_two == "rock":
print ("Player Two Wins!")
elif player_one == "scissors" and player_two == "paper":
print ("Player One Wins!")
elif player_one == player_two:
print ("Time Game!")
else:
print("Something went wrong!")
然而,当我用“石头”、“纸”或“剪刀”以外的东西来回应玩家的回应时,PowerShell会出现以下错误。例如,当我输入“rocks”时,我会得到:
Traceback (most recent call last):
File "rpsbasic3.py", line 5, in <module>
player_one = input("Player One, make your move! ")
File "<string>", line 1, in <module>
NameError: name 'rocks' is not defined
我搞了一段时间了,不知所措。