我想模拟一个石头,布,剪刀的游戏,这就是我目前所拥有的。它不允许我在scoregame
函数中输入字母。我该怎么解决这个问题?在
def scoregame(player1, player2):
if player1 == R and player2 == R:
scoregame = "It's a tie, nobody wins."
if player1 == S and player2 == S:
scoregame == "It's a tie, nobody wins."
if player1 == P and player2 == P:
scoregame = "It's a tie, nobody wins."
if player1 == R and player2 == S:
scoregame = "Player 1 wins."
if player1 == S and player2 == P:
scoregame = "Player 1 wins."
if player1 == P and player2 == R:
scoregame = "Player 1 wins."
if player1 == R and player2 == P:
scoregame == "Player 2 wins."
if player1 == S and player2 == R:
scoregame == "Player 2 wins."
if player1 == P and player2 == S:
scoregame = "Player 2 wins."
print(scoregame)
您使用的是没有引号的字母,因此它需要一个名为p的变量,但您需要的是一个字符串“p”,因此请将字母用引号括起来:
您需要针对字符串进行测试;现在针对变量名进行测试:
但是你可以通过测试是否相等来简化两个玩家选择相同选项的情况:
^{pr2}$接下来,我会用一个地图,一本字典来编纂什么胜过什么:
现在你的游戏可以在两个测试中测试。总而言之:
相关问题 更多 >
编程相关推荐