预测球队比赛成绩

预测球队比赛成绩

乒乓球比赛规则:

       在一局比赛中,先得11分的一方为胜方,10平后,先多得2分的一方为胜方。(T2赛制24分钟限时,24分钟前11分即获胜无需多得2分即使10:10也只需要再得1分即胜利,24分钟后采用5分制,即谁先得5分即胜利)。强调:一定要有一人达到十分后,领先2分,才算胜方。如14:15不行,8:5也不行,但11:13是13分的为胜方.(T2新赛制24分钟前11分制,24分钟后5分制,而且无需领先2分)
       一场比赛应采用三局两胜制或五局三胜制或七局四胜制。一场比赛应连续进行,但在局与局之间,任何一名运动员都有权要求不超过一分钟的休息时间

 代码:

 1 from random import random
 2 def printIntro():
 3     print("兵乓球比赛结果预测")
 4 def getInputs():
 5     a = eval(input("请输入选手A的能力值(0-1): "))
 6     b = eval(input("请输入选手B的能力值(0-1): "))
 7     x = eval(input("模拟比赛的场次: "))
 8     return a, b, x
 9 
10 def simNGames(x, probA, probB):
11     winsA, winsB = 0, 0
12     for i in range(x):
13         scoreA, scoreB = simOneGame(probA, probB)
14         print(scoreA,scoreB)
15         if scoreA > scoreB:
16             winsA += 1
17         else:
18             winsB += 1
19     return winsA, winsB
20 def simOneGame(probA, probB):
21     scoreA, scoreB = 0, 0
22     serving = "A"
23     while not gameOver(scoreA, scoreB):
24         if serving == "A":
25             if random() < probA:
26                 scoreA += 1
27             else:
28                 serving="B"
29         else:
30             if random() < probB:
31                 scoreB += 1
32             else:
33                 serving="A"
34     
35     return scoreA, scoreB
36 def gameOver(a,b):
37     if (a>=11 and abs(a-b)>=2) or (b>=11 and abs(a-b)>=2):
38         return True
39         
40 def printSummary(winsA, winsB):
41     x = winsA + winsB
42     print("竞技分析开始,共模拟{}场比赛".format(x))
43     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/x))
44     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/x))
45 def main():
46     printIntro()
47     probA, probB, x = getInputs()
48     winsA, winsB = simNGames(x, probA, probB)
49     printSummary(winsA, winsB)
50 main()

结果:

 

 

 

 

 

posted @ 2020-05-13 19:32  酒久  阅读(153)  评论(0编辑  收藏  举报