用python进行对乒乓球比赛的分析
在一场比赛中,先得11分的获胜;若俩队同时的10分,则先超过对方2分的获胜。
单打采用7局4胜,双打采用5局3胜。
(1)单打
代码:
1 #e15.1MatchAnalysis.py 2 from random import random 3 def printIntro(): 4 print("01号ghh进行单打比赛分析(七局四胜):") 5 print("这个程序模拟两个选手A和B的某种竞技比赛") 6 print("程序运行需要A和B的能力值(以0到1之间的小数表示)") 7 def getInputs(): 8 a = eval(input("请输入选手A的能力值(0-1): ")) 9 b = eval(input("请输入选手B的能力值(0-1): ")) 10 n = eval(input("模拟比赛的场次: ")) 11 return a, b, n 12 def simNGames(n, probA, probB): 13 #进行N场比赛 14 winsA, winsB = 0, 0 15 for i in range(n): 16 for j in range(7):#进行7局4胜的比赛 17 scoreA, scoreB = simOneGame(probA, probB) 18 if scoreA > scoreB: 19 winsA += 1 20 else: 21 winsB += 1 22 return winsA, winsB 23 def gameOver1(a,b):#先得11分的获胜 24 return a==11 or b==11 25 def gameOver2(a,b):#两人都得10分后的结束标志 26 if abs(a-b)>=2: 27 return a,b 28 def simOneGame(probA, probB): 29 scoreA, scoreB = 0, 0 30 serving = "A" 31 while not gameOver1(scoreA, scoreB): 32 if scoreA==10 and scoreB==10: 33 return simtwoGame(probA,probB) 34 if serving == "A": 35 if random() < probA: 36 scoreA += 1 37 else: 38 serving="B" 39 else: 40 if random() < probB: 41 scoreB += 1 42 else: 43 serving="A" 44 return scoreA, scoreB 45 def simtwoGame(probA,probB):#双方都得10分后的一场比赛 46 scoreA,scoreB=10,10 47 serving = "A" 48 while not gameOver2(scoreA, scoreB): 49 if serving == "A": 50 if random() < probA: 51 scoreA += 1 52 else: 53 serving="B" 54 else: 55 if random() < probB: 56 scoreB += 1 57 else: 58 serving="A" 59 return scoreA, scoreB 60 def printSummary(winsA, winsB): 61 n = winsA + winsB 62 print("竞技分析开始,共模拟{}场比赛".format(n)) 63 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) 64 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) 65 def main(): 66 printIntro() 67 probA, probB, n = getInputs() 68 winsA, winsB = simNGames(n, probA, probB) 69 printSummary(winsA, winsB) 70 main()
结果:
(2)双打
代码:
1 from random import random 2 def printIntro(): 3 print("01号ghh进行双打比赛团体赛分析(5局3胜):") 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 #进行N场比赛 13 winsA, winsB = 0, 0 14 for i in range(n): 15 for j in range(5):#进行5局3胜的比赛 16 scoreA, scoreB = simOneGame(probA, probB) 17 if scoreA > scoreB: 18 winsA += 1 19 else: 20 winsB += 1 21 return winsA, winsB 22 def gameOver1(a,b):#先得11分的获胜 23 return a==11 or b==11 24 def gameOver2(a,b):#两队都得10分后的结束标志 25 if abs(a-b)>=2: 26 return a,b 27 def simOneGame(probA, probB): 28 scoreA, scoreB = 0, 0 29 serving = "A" 30 while not gameOver1(scoreA, scoreB): 31 if scoreA==10 and scoreB==10: 32 return simtwoGame(probA,probB) 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 simtwoGame(probA,probB):#双方都得10分后的一场比赛 45 scoreA,scoreB=10,10 46 serving = "A" 47 while not gameOver2(scoreA, scoreB): 48 if serving == "A": 49 if random() < probA: 50 scoreA += 1 51 else: 52 serving="B" 53 else: 54 if random() < probB: 55 scoreB += 1 56 else: 57 serving="A" 58 return scoreA, scoreB 59 def printSummary(winsA, winsB): 60 n = winsA + winsB 61 print("竞技分析开始,共模拟{}场比赛".format(n)) 62 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) 63 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) 64 def main(): 65 printIntro() 66 probA, probB, n = getInputs() 67 winsA, winsB = simNGames(n, probA, probB) 68 printSummary(winsA, winsB) 69 main()
结果:
(3)用pyinstall库打包
在命令行中输入Pyinstaller -F 再加上你写的文件的路径
具体如下:
执行结果: