[英]python while loop point system - running slow
I have to simulate a points game where the first person to reach 11 points and win by 2 clear points wins the game 我必须模拟一个点数游戏,其中第一个达到11点并以2个净分获胜的人赢得了游戏
I have used a function to decide who wins a point 我已经使用一个函数来决定谁赢得积分
def simulatePoint(winProbabilityA, winProbabilityB):
rNumber = random.random()
if rNumber - winProbabilityA <= 0:
# A wins the game
return 0
elif rNumber - winProbabilityA > 0:
# B wins the game
return 1
and another one to simulate a game 另一个模拟游戏
def simulateGame (playerA_winProb, playerB_winProb):
gameWon = False
pointsA = 0
pointsB = 0
while gameWon == False:
# Simulate point
point = simulatePoint(playerA_winProb, playerB_winProb)
if point == 0:
pointsA += 1
elif point == 1:
pointsB += 1
# Checks for points to be equal to 11
if (pointsA == 11) and (pointsA - pointsB >= 2):
# A wins the game
gameWon = True
return 0
elif (pointsB == 11) and (pointsB - pointsA >= 2):
# B wins the game
gameWon = True
return 1
This is where i believe i am going wrong, i think the while loop is causing the code to run slow 这是我认为我做错了的地方,我认为while循环导致代码运行缓慢
Any help is greatfully accepted 任何帮助都可以接受
What if the difference becomes greater than or equal to 2, after they have won more than 11 games. 如果他们赢得11场以上的比赛之后,差值大于或等于2怎么办? So, the logic should have been like this
所以逻辑应该是这样的
if (pointsA >= 11) and (pointsA - pointsB >= 2):
...
elif (pointsB >= 11) and (pointsB - pointsA >= 2):
...
I think your code is running infinitely. 我认为您的代码正在无限运行。
Consider: pointsA
and pointsB
reach 10
and 10
respectively. 考虑:点
pointsA
和点pointsB
分别达到10
和10
。 Now, no matter what player gets the next point, neither of your terminating conditions will be reached because neither pointsA
nor pointsB
will be 11
and up by 2 at the same time. 现在,无论哪个玩家获得下一个分数,都不会达到您的终止条件,因为
pointsA
和pointsB
都不会同时变为11
和2。 This creates an infinite loop. 这将创建一个无限循环。
You'd probably want to check if pointsA >= 11
and pointsB >= 11
instead of A == 11
and B == 11
. 您可能想检查点
pointsA >= 11
和点pointsB >= 11
而不是A == 11
和B == 11
。
Looks like thefourtheye beat me by a bit - he gets my vote. 看起来thefourtheye击败了我-他赢得了我的选票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.