其他分享
首页 > 其他分享> > 预测选手比赛成绩

预测选手比赛成绩

作者:互联网

 1 from random import random
 2 def printIntro():
 3     print("这个程序模拟两个选手A和B的某种竞技比赛")
 4     print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
 5 def getInputs():
 6     a = eval(input("请输入选手A的能力值(0-1): "))
 7     b = eval(input("请输入选手B的能力值(0-1): "))
 8     n = eval(input("模拟比赛的场次: "))
 9     return a, b, n
10 def simNGames(n, probA, probB):
11     winsA, winsB = 0, 0
12     for i in range(n):
13         scoreA, scoreB = simOneGame(probA, probB)
14         if scoreA > scoreB:
15             winsA += 1
16         else:
17             winsB += 1
18     return winsA, winsB
19 def gameOver(a,b):
20     return a==15 or b==15
21 def simOneGame(probA, probB):
22     scoreA, scoreB = 0, 0
23     serving = "A"
24     while not gameOver(scoreA, scoreB):
25         if serving == "A":
26             if random() < probA:
27                 scoreA += 1
28             else:
29                 serving="B"
30         else:
31             if random() < probB:
32                 scoreB += 1
33             else:
34                 serving="A"
35     return scoreA, scoreB
36 def printSummary(winsA, winsB):
37     n = winsA + winsB
38     print("竞技分析开始,共模拟{}场比赛".format(n))
39     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
40     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
41 def main():
42     printIntro()
43     probA, probB, n = getInputs()
44     winsA, winsB = simNGames(n, probA, probB)
45     printSummary(winsA, winsB)
46 main()

标签:winsB,winsA,比赛,probB,probA,选手,成绩,def,scoreA
来源: https://www.cnblogs.com/wendy123/p/12751305.html