为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

www说 2024-01-27 15:24:31
如您所见,我是 Python 的初学者,因此我们将不胜感激。我的问题是,我正在尝试测试所有场景的代码,但我无法测试决胜局。当然,我可以只插入Player1Score = Player2Score(我已经对其进行了哈希标记以显示位置),但这只会使程序进入无限循环,这违背了决胜局的目的。那么有什么办法可以让程序只经历一次决胜局部分,然后让一个玩家获胜呢?(如果我的问题有任何错误,我深表歉意,我也是 stackoverflow 的新手)import randomdef 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 Score2DiceGame()
查看完整描述

1 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

如果您确实愿意,您可以添加临时玩家分数来测试该功能,然后如果您发现它们有效,则可以再次将其删除。通常运行它就足够了,但正如你提到的,它会永远循环。我想,这确实表明它是有效的,但我同意这不是最佳的。


def DiceGame(count, p1, p2):

  Count = count

  Player1Score = p1

  Player2Score = p2

  ...

然后在文件底部将其称为DiceGame(5, 1, 1),并在决胜局中将其称为DiceGame(0, 0, 0)。这将在第一次运行时强制平局,并在第二次运行时正常运行。


if Player1Score == Player2Score:

    print ("It is a tie!")

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

    Count -= 1

    DiceGame(0, 0, 0)


... #  code inbetween


#  end of file

    return score2


DiceGame(5, 1, 1)


查看完整回答
反对 回复 2024-01-27
  • 1 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信