我能做些什么来修复我的石头、纸、剪刀代码以保持分数并结束游戏



这是我的石头,纸,剪刀游戏的代码,我对如何创建运行分数以及用户输入0后结束游戏的方法感到困惑。

import random
def main():
  print ("Welcome to the Rock, Paper, Scissors Tournament!")
  computerTotal = 0
  playerTotal = 0
  playerChoice = playerSelection()
  while (playerChoice != 0):
      computerChoice = computerSelection()
      winner = roundWinner()
      if (winner == 'computer'):
          computerTotal = computerTotal + 1
      elif (winner == 'player'):
          playerTotal = playerTotal + 1
      playerChoice = playerSelection()
      matchWinner = (computerTotal, playerTotal)

def computerSelection():
    choice = random.randint(1,3)
    if choice == 1:
      print ("Computer chose rock")
    elif choice == 2:
      print ("Computer chose scissors")
    elif choice == 3 :
      print ("Computer chose paper")
    return choice
def playerSelection():
    choice = input ("Enter 1 for rock, 2 for scissors, 3 for paper (0 to end the Tournament): ")
    if choice == 0:
      print  ('Final score:', matchWinner() )
    elif choice == 1:
      print ('Player chose rock')
    elif choice == 2:
      print ('Player chose scissors')
    elif choice == 3:
      print ("Player chose paper")
    return playerSelection
def roundWinner():
  if playerSelection() == computerSelection():
    print("Draw no one wins!")
  elif playerSelection == 1 and computerSelection == 3:
    print("Computer Wins!")
  elif playerSelection == 1 and computerSelection == 2:
    print("Player Wins!")
  elif playerSelection == 3 and computerSelection == 1:
    print("Player Wins!")
  elif playerSelection == 3 and computerSelection == 2:
    print("Computer Wins!")
  elif playerSelection == 2 and computerSelection == 1:
    print("Computer Wins!")
  elif playerSelection == 2 and computerSelection == 3:
    print("Player Wins!")

def matchWinner():
  if computerTotal > playerTotal :
    print (" Computer wins the game!" )
  elif computerTotal == playerTotal:
    print (" Draw no one wins!" )
  elif computerTotal < playerTotal:
    print (" Player wins the game!" )

我还想在用户键入 0 并且游戏结束后显示比赛获胜者。

你的循环很糟糕。

  • 删除 : computerChoice = computerSelection()playerChoice = playerSelection(),因为它们是以 roundWinner 计算的。您实际上每个循环进行 2 次播放。

  • 无缩进 : matchWinner(computerTotal, playerTotal)

while True:
    winner = roundWinner()
    if (winner == 'computer'):
        computerTotal = computerTotal + 1
    elif (winner == 'player'):
        playerTotal = playerTotal + 1
    elif (winner == 'exit'):
        break
matchWinner(computerTotal, playerTotal)

还修复了roundWinner在玩家按 0 的情况下返回"退出"。在这种情况下,退出决定应该上升到所有更高的职能。

最新更新