【python】体育竞技分析:预测球队比赛成绩

乒乓球比赛预测

规则:再一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。单打的淘汰赛以七局四胜制,双打淘汰赛和团体赛采用五局三胜制。

 1 from random import random
 2 def printInfo():
 3     print("2019310143031")
 4     print("这个程序模拟两个选手A和B的乒乓比赛")
 5     print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
 6 def getInputs():
 7     a = eval(input("请输入选手A的能力值(0-1): "))
 8     b = eval(input("请输入选手B的能力值(0-1): "))
 9     n = eval(input("模拟比赛的场次: "))
10     return a, b, n
11 def simNGames(n, probA, probB):
12     winsA, winsB = 0, 0
13     for i in range(n):
14         scoreA, scoreB = simOneGame(probA, probB)
15         if scoreA > scoreB:
16             winsA += 1
17         else:
18             winsB += 1
19     return winsA, winsB
20 def gameOver(a,b):
21     if a>=10 and b>=10:
22          if abs(a-b)==2:
23              return 1
24     elif a<10 and b<10:
25          if a==11 or b==11:
26              return 1
27     else:
28          return 0
29 def simOneGame(probA,probB):
30     scoreA,scoreB=0,0
31     serving="A"
32     while not gameOver(scoreA,scoreB):
33          if serving=="A":
34              if random()<probA:
35                  scoreA+=1
36              else:
37                  serving="B"
38          else:
39              if random()<probB:
40                  scoreB+=1
41              else:
42                  serving="A"
43          return scoreA,scoreB
44 def printSummary(winsA,winsB):
45      n=winsA+winsB
46      print("竞技分析开始,一共模拟{}场比赛".format(n))
47      print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
48      print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
49 def main():
50     printInfo()
51     probA,probB,n=getInputs()
52     winsA,winsB=simNGames(n,probA,probB)
53     printSummary(winsA,winsB)
54 main()
55 python3: input("please input any key to exit!")
原文地址:https://www.cnblogs.com/litchi666/p/12857011.html