Python 记分环

Python 记分环,python,python-3.x,Python,Python 3.x,我正在为一个石头剪刀游戏编写代码,它有一个1-3之间的随机数发生器,模拟计算机的投掷,它工作得非常好。我想做的是为3个不同的分数添加一个计分系统: 用户赢 康普温 八字游戏 我还有一个循环,可以让你玩多种游戏 但是我找不到一种在比赛中更新比分的方法 非常感谢您的帮助,您可以定义一个全局变量: gamesPlayed = 0 userWins = 0 compWins = 0 def playOneRound(): global gamesPlayed global u

我正在为一个石头剪刀游戏编写代码,它有一个1-3之间的随机数发生器,模拟计算机的投掷,它工作得非常好。我想做的是为3个不同的分数添加一个计分系统:

  • 用户赢
  • 康普温
  • 八字游戏
我还有一个循环,可以让你玩多种游戏

但是我找不到一种在比赛中更新比分的方法


非常感谢您的帮助,

您可以定义一个全局变量:

gamesPlayed = 0   
userWins = 0
compWins = 0    

def playOneRound():
  global gamesPlayed
  global userWins
  global compWins
  compThrow = getCompThrow()
  userThrow = getUserThrow()
  result = compareThrows(userThrow, compThrow)
  if result == "Win":
    print("Winner Winner Chicken Dinner")
    print("-------------------------------------------------")
    userWins += 1
  elif result == "Lose":
    print("Loser Loser Chicken Loser ")
    print("-------------------------------------------------")
    compWins += 1
  else:
    print("Tie")
    print("-------------------------------------------------")
  gamesPlayed += 1
第二,也许是更好的方法:

class Scores:
    def __init__(self):
        self.gamesPlayed = 0
        self.userWins = 0
        self.compWins = 0

scores = Scores()

def playOneRound():
  compThrow = getCompThrow()
  userThrow = getUserThrow()
  result = compareThrows(userThrow, compThrow)
  if result == "Win":
    print("Winner Winner Chicken Dinner")
    print("-------------------------------------------------")
    scores.userWins += 1
  elif result == "Lose":
    print("Loser Loser Chicken Loser ")
    print("-------------------------------------------------")
    scores.compWins += 1
  else:
    print("Tie")
    print("-------------------------------------------------")
  scores.gamesPlayed += 1

我以为是Python3,请解释一下不同的解释。我编辑了你明显不正确的标签。将它设为一个类,并将分数设为该类的一个变量,并为每个游戏增加分数。通常应避免使用全局变量。所需的代码更改将更加普遍,但这给我的印象是一种懒惰的逃避。我认为最好的方法是将此代码放入类中,但使用全局变量的方法是我假设的快速且肮脏的方法;)