计分循环

时间:2018-12-04 10:31:47

标签: python python-3.x

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

  • userWins
  • compWins
  • 玩过的游戏

我还有一个循环,可让您玩多种游戏。

但是我找不到一种在演奏时更新乐谱的方法。

任何帮助将不胜感激,

1 个答案:

答案 0 :(得分:0)

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

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