如您所見,我是 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)
添加回答
舉報
0/150
提交
取消