我如何能够测试下面的代码,以便我可以看到决胜局如何发挥作用?

如您所见,我是 Python 的初学者,因此我们将不胜感激。我的问题是,我正在尝试测试所有场景的代码,但我无法测试决胜局。当然,我可以只插入Player1Score = Player2Score(我已经对其进行了哈希标记以显示位置),但这只会使程序进入无限循环,这违背了决胜局的目的。那么有什么办法可以让程序只经历一次决胜局部分,然后让一个玩家获胜呢?

(如果我的问题有任何错误,我深表歉意,我也是 stackoverflow 的新手)

import random


def DiceGame():

  Count = 0

  Player1Score = 0

  Player2Score = 0

  while Count <= 4:

    Count += 1

    print ("\n It is Round",Count, "\n")

    print ("It is Player 1's turn.")

    x = input("Press [Enter] to roll.")

    Score = Rolls()

    Player1Score += Score

    print ("Player 1, your score so far is",Player1Score)

    print ("It is Player 2's turn.")

    x = input("Press [Enter] to roll.")

    Score = Rolls()

    Player2Score += Score

    print ("Player 2, your score so far is",Player2Score)

  #Player1Score = Player2Score

  if Player1Score == Player2Score:

    print ("It is a tie!")

    print ("There will be a final tiebreaker.")

    Count -= 1

    DiceGame()

  elif Player1Score >= Player2Score:

    print ("Player 1 wins!")

  elif Player1Score <= Player2Score:

    print ("Player 2 wins!")



def Rolls():

  Roll1 = random.randint(1,6)

  Roll2 = random.randint(1,6)

  print ("You got a",Roll1)

  print ("You got a",Roll2)

  Score1 = Roll1 + Roll2

  if Score1 == 2 or Score1 == 4 or Score1 == 6 or Score1 == 8 or Score1 == 10 or Score1 == 12:

    print ("Your total is even so you get an extra 10 pts.")

    Score2 = Score1 + 10

    print ("Your score for this round is" ,Score2)

  elif Score1 == 3 or Score1 == 5 or Score1 == 7 or Score1 == 9 or Score1 == 11:

    print ("Your total is odd so you lose 5 pts.")

    Score2 = Score1 - 5

    if Score2 <= 0:

      print ("Your score has gone below 0pts. It will therefore be reset to 0pts")

      Score2 = 0

    print ("Your score for this round is" ,Score2)

  

  return Score2


DiceGame()


www说
浏览 96回答 1
1回答

慕尼黑5688855

如果您确实愿意,您可以添加临时玩家分数来测试该功能,然后如果您发现它们有效,则可以再次将其删除。通常运行它就足够了,但正如你提到的,它会永远循环。我想,这确实表明它是有效的,但我同意这不是最佳的。def DiceGame(count, p1, p2):&nbsp; Count = count&nbsp; Player1Score = p1&nbsp; Player2Score = p2&nbsp; ...然后在文件底部将其称为DiceGame(5, 1, 1),并在决胜局中将其称为DiceGame(0, 0, 0)。这将在第一次运行时强制平局,并在第二次运行时正常运行。if Player1Score == Player2Score:&nbsp; &nbsp; print ("It is a tie!")&nbsp; &nbsp; print ("There will be a final tiebreaker.")&nbsp; &nbsp; Count -= 1&nbsp; &nbsp; DiceGame(0, 0, 0)... #&nbsp; code inbetween#&nbsp; end of file&nbsp; &nbsp; return score2DiceGame(5, 1, 1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python