你需要测试strings;您现在正在测试变量名称:
if player1 == 'R' and player2 == 'R':
但是你可以通过测试两个玩家是否相等来简化两个玩家选择相同选项的情况:
if player1 == player2:
scoregame = "It's a tie, nobody wins."
接下来,我将使用映射、字典来整理哪些内容胜过哪些内容:
beats = {'R': 'S', 'S': 'P', 'P': 'R'}
if beats[player1] == player2:
scoregame = "Player 1 wins."
else:
scoregame = "Player 2 wins."
现在您的游戏只需进行 2 次测试即可进行测试。全部放在一起:
def scoregame(player1, player2):
beats = {'R': 'S', 'S': 'P', 'P': 'R'}
if player1 == player2:
scoregame = "It's a tie, nobody wins."
elif beats[player1] == player2:
scoregame = "Player 1 wins."
else:
scoregame = "Player 2 wins."
print(scoregame)