我能做些什么来修正我的石头,布,剪子代码来记分和结束游戏

2024-09-30 12:16:34 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的一个石头,布,剪刀游戏的代码,我很困惑如何创建一个运行分数,以及如何结束游戏,一旦用户输入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并且游戏结束后,我还想显示比赛赢家。你知道吗


Tags: andtheifdefcomputerplayerprintchoice
2条回答

你的循环不好。你知道吗

  • 删除: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时返回'exit'。在这种情况下,退出决定应该上升到所有更高的职能部门。你知道吗

我试过修改一下你的代码,但效果不太好,我建议你下次在这种情况下少用一些函数,这样会让你的生活更轻松。 不管怎样,我把你的代码(做了一些小改动)放在一个函数中。你知道吗

import random
def RPS():
    computerTotal = 0
    playerTotal = 0
    playerChoice = input ("Enter 1 for rock, 2 for scissors, 3 for paper (0 to end the Tournament): ")
    while (playerChoice != 0):
        computerChoice = random.randint(1,3)

        #Prints R/P/S of player & computer.

        if computerChoice == 1:
            print ("Computer chose rock")
        elif computerChoice == 2:
            print ("Computer chose scissors")
        elif computerChoice == 3 :
            print ("Computer chose paper")
        if playerChoice == 1:
            print ('Player chose rock')
        elif playerChoice == 2:
            print ('Player chose scissors')
        elif playerChoice == 3:
            print ("Player chose paper")

        #Calculating & printing who won(the round)

        if playerChoice == computerChoice:
            print("Draw no one wins!")
        elif playerChoice == 1 and computerChoice == 3:
            print("Computer Wins!")
            computerTotal = computerTotal + 1
        elif playerChoice == 1 and computerChoice == 2:
            print("Player Wins!")
            playerTotal = playerTotal + 1
        elif playerChoice == 3 and computerChoice == 1:
            print("Player Wins!")
            playerTotal = playerTotal + 1
        elif playerChoice == 3 and computerChoice == 2:
            print("Computer Wins!")
            computerTotal = computerTotal + 1
        elif playerChoice == 2 and computerChoice == 1:
            print("Computer Wins!")
            computerTotal = computerTotal + 1
        elif playerChoice == 2 and computerChoice == 3:
            print("Player Wins!")
            playerTotal = playerTotal + 1

        playerChoice = input ("Enter 1 for rock, 2 for scissors, 3 for paper (0 to end the Tournament): ")

      #if we got here means the player entered 0. printing who won the game
    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时,它将中断循环并显示结果。你知道吗

相关问题 更多 >

    热门问题