简体   繁体   English

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

[英]What can I do to fix my Rock, Paper, Scissors code to keep score and to end the game

Here's my code for a Rock, Paper, Scissors game and I am confused on how to create a running score as well as a way to end the game once the user enters 0.这是我的石头剪刀布游戏代码,我对如何创建跑分以及在用户输入 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!" )

I want to also display the match winner once the user has typed 0 and the game has ended.一旦用户输入 0 并且游戏结束,我还想显示比赛获胜者。

Your loop is bad. 您的循环不好。

  • Remove : computerChoice = computerSelection() and playerChoice = playerSelection() because they are calculated in roundWinner . 删除: computerChoice = computerSelection()playerChoice = playerSelection()因为它们是在roundWinner中计算的。 Your are actually doing 2 plays per loop. 您实际上每个循环执行2次播放。

  • Unindent : matchWinner(computerTotal, playerTotal) Unindent: 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)

Also fix roundWinner to return 'exit' in case player pressed 0. In that case, the exit decision should rise up to all higher functions. 还要修复roundWinner在玩家按下0的情况下返回“退出”的情况。在这种情况下,退出决定应上升到所有更高的功能。

I've tried changing a bit your code and it didn't really go well, I'd suggest you to use less functions next time in cases like that, it will make your life easier. 我尝试过更改一下代码,但实际上并不能很好地执行,建议您下次在这种情况下使用较少的函数,这样会使您的生活更轻松。 Anyway, I took your code (with a few minor changes) and put it all in one function. 无论如何,我接受了您的代码(进行了一些小的更改)并将其全部放在一个函数中。

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!" )

As you can see, I didn't use a function to run the player Selection and I made it so whenever you enter 0 it will break the loop and show the results. 如您所见,我没有使用函数来运行播放器Selection,而是做到了,因此只要您输入0,它就会中断循环并显示结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在石头剪刀布游戏中记分? - How do I keep score in a rock paper scissors game? 如何用更少的代码行制作我的基本的 Rock/Paper/Scissors 游戏? - How do I make my basic Rock/Paper/Scissors game with fewer lines of code? 如何使我的Rock,Paper和剪刀游戏的代码不那么多余? - How can I make my code for a game of Rock, Paper, and scissors less redundant? 我正在使用 Tkinter 在 Python 中制作剪刀石头布游戏。 我的分数工作不正常 - I'm Making a Rock Paper Scissors game in Python with Tkinter. My Score isn't Working Properly 尝试使用 tkinter 编写“剪刀石头布”游戏。 但是我的编码没有给出我想要的,并且没有发生错误 - Trying to code 'rock paper scissors' game with tkinter. But my coding doesn't give what i want, and no error has occured 我该怎么办这个石头剪刀布游戏我只是不知道从这里开始? - what do i do with this rock paper scissors game i just don't where to go with it from here? 如何用Python完成此“剪刀石头布”游戏? - How do I finish this Rock, Paper, Scissors game in Python? 程序运行但是当我输入我的值时它什么也不做。 这是石头剪刀布游戏 - the program runs but when i type in my value it doesnt do anything . this is a game of rock paper scissors python - 我在哪里检查我的石头剪刀布游戏中的有效输入 - python - Where do I check for valid input in my rock paper scissors game 如何在我的石头剪刀布游戏中再添加一轮 - How can I add another round to my Rock Paper Scissors game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM