用python进行对乒乓球的比赛分析,并且将该程序进行封装,python乒乓球,
用python进行对乒乓球的比赛分析,并且将该程序进行封装,python乒乓球,
2、单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。
重点:
思维方式:自顶向下即将一个复杂问题分解成几个问题,再细分成一个个具体的小问题,从而来解决复杂问题。自底向上为自顶向下的逆过程,即解决复杂问题的方法,逐步解决一个个小问题,来达成目的。
2.将体育竞技分析分解为以下几个小步骤
1、打印程序的介绍性信息式
2、获得程序运行参数:probA(A的能力值),probB(B的能力值),n(比赛场次)
3、利用球员AB的能力值,模拟n场比赛
4、输出球员AB获胜的场次及概率
3.将各个步骤定义成函数来实现:
分解代码
from random import random #引用随机库def printIntro(): #打印程序介绍信息 print("这个程序模拟两个选手A和B的某种竞技比赛") print("程序运行需要A和B的能力值(以0到1之间的小数表示)")def getInputs(): #获得程序运行参数 a = eval(input("请输入选手A的能力值(0-1): ")) b = eval(input("请输入选手B的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a, b, ndef simNGames(n, probA, probB): #进行N场比赛 每场比赛是7局4胜制 def gameOver(a,b): #正常比赛结束 def gameOver2(a,b): #抢12比赛结束 def simOneGame(probA, probB): #进行一场比赛 def simtwoGame2(probA,probB): #进行抢12比赛 def printSummary(winsA, winsB): #打印程序结果信息 n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))def main():
完整的代码如下:
from random import randomdef printIntro(): #打印程序介绍信息 print("信计一班10号石贵雨进行比赛分析结果:") print("这个程序模拟两个选手A和B的某种竞技比赛") print("程序运行需要A和B的能力值(以0到1之间的小数表示)")def getInputs(): #获得程序运行参数 a = eval(input("请输入选手A的能力值(0-1): ")) b = eval(input("请输入选手B的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a, b, ndef simNGames(n, probA, probB): # 进行N场比赛 winsA, winsB = 0, 0 for i in range(n): for j in range(7): #进行7局4胜的比赛 scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsBdef gameOver(a,b): #正常比赛结束 return a==11 or b==11def gameOver2(a,b): #进行抢12比赛结束 if abs((a-b))>=2 return a,bdef simOneGame(probA, probB): #进行一场比赛 scoreA, scoreB = 0, 0 #初始化AB的得分 serving = "A" while not gameOver(scoreA, scoreB): #用while循环来执行比赛 if scoreA==10 and scoreB==10: return(simtwoGame2(probA,probB)) if serving == "A": if random() < probA: ##用随机数生成胜负 scoreA += 1 else: serving="B" else: if random() < probB: scoreB += 1 else: serving="A" return scoreA, scoreBdef simtwoGame2(probA,probB): scoreA,scoreB=10,10 serving = "A" while not gameOver2(scoreA, scoreB): if serving == "A": if random() < probA: scoreA += 1 else: serving="B" else: if random() < probB: scoreB += 1 else: serving="A" return scoreA, scoreBdef printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB)main()
4、模拟1000场单打7局4胜的比赛情况结果:
5.模拟团队打5局3胜的情况:
from random import random def printIntro(): #打印程序介绍信息 print("信计一班13号郑志杰进行比赛分析结果:") print("这个程序模拟两个队伍A和B的某种竞技比赛") print("程序运行需要队伍A和队伍B的能力值(以0到1之间的小数表示)")def getInputs(): #获得程序运行参数 a = eval(input("请输入队伍A的能力值(0-1): ")) b = eval(input("请输入队伍B的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a, b, ndef simNGames(n, probA, probB): # 进行N场比赛 winsA, winsB = 0, 0 for i in range(n): for j in range(7): #进行7局4胜的比赛 scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsBdef gameOver(a,b): #正常比赛结束 return a==11 or b==11def gameOver2(a,b): #进行抢12比赛结束 return a==12 or b==12def simOneGame(probA, probB): #进行一场比赛 scoreA, scoreB = 0, 0 #初始化AB的得分 serving = "A" while not gameOver(scoreA, scoreB): #用while循环来执行比赛 if scoreA==10 and scoreB==10: return(simtwoGame2(probA,probB)) if serving == "A": if random() < probA: ##用随机数生成胜负 scoreA += 1 else: serving="B" else: if random() < probB: scoreB += 1 else: serving="A" return scoreA, scoreBdef simtwoGame2(probA,probB): scoreA,scoreB=10,10 serving = "A" #假如先让队伍A发球 while not gameOver2(scoreA, scoreB): if serving == "A": if random() < probA: scoreA += 1 else: serving="B" else: if random() < probB: scoreB += 1 else: serving="A" return scoreA, scoreBdef printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("队伍A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("队伍B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB)
模拟1000场打5局3胜的比赛情况
模拟篮球比赛:规则7局4胜,同样是先得11分的一方为胜,10平的话就要进行抢12的比赛
from random import random def printIntro(): #打印程序介绍信息 print("信计一班13号郑志杰进行比赛分析结果:") print("这个程序模拟两个篮球队伍A和B的某种竞技比赛") print("程序运行需要篮球队伍A和队伍B的能力值(以0到1之间的小数表示)")def getInputs(): #获得程序运行参数 a = eval(input("请输入篮球队伍A的能力值(0-1): ")) b = eval(input("请输入篮球队伍B的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a, b, ndef simNGames(n, probA, probB): # 进行N场比赛 winsA, winsB = 0, 0 for i in range(n): for j in range(7): #进行7局4胜的比赛 scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsBdef gameOver(a,b): #正常比赛结束 return a==11 or b==11def gameOver2(a,b): #进行抢12比赛结束 return a==12 or b==12def simOneGame(probA, probB): #进行一场比赛 scoreA, scoreB = 0, 0 #初始化AB的得分 serving = "A" while not gameOver(scoreA, scoreB): #用while循环来执行比赛 if scoreA==10 and scoreB==10: return(simtwoGame2(probA,probB)) if serving == "A": if random() < probA: ##用随机数生成胜负 scoreA += 1 else: serving="B" else: if random() < probB: scoreB += 1 else: serving="A" return scoreA, scoreBdef simtwoGame2(probA,probB): scoreA,scoreB=10,10 serving = "A" #假如先让队伍A进攻 while not gameOver2(scoreA, scoreB): if serving == "A": if random() < probA: scoreA += 1 else: serving="B" else: if random() < probB: scoreB += 1 else: serving="A" return scoreA, scoreBdef printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("篮球队伍A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("篮球队伍B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB)main()
比赛结果如图:
6、最后将该程序进行封装
1、 首先要安装pyinstall库,在cmd中用输入pip install PyInstaller
2.接着会自动下载,安装成功后 通过Pyinstall自带命令进行打包
安装过程就不截图了。
3.控制台输入 Pyinstaller -F xxx(pyw文件路径,例如c://user/desktop/demo.py)
4.打包后exe文件路径在项目下dict文件夹中 具体可以看日志
具体情况:
执行情况:
在dict文件下可查看该exe程序
用python进行对乒乓球的比赛分析,并且将该程序进行封装
相关内容
- Python 二级模拟操作题(八),,1. 从键盘输入一个
- python3执行javaScript脚本,js2py教程,,简介:js2py安装
- python eval()内置函数,,python有一个内
- Python第一次做上传文件,python上传文件,把第一次做的上
- Python—模块-time、random,,import 1、执
- Python 安装第三方库,python第三方库,cmd进入Pytho
- Python打包-Pyinstaller,python-pyinstaller, 我们知道,Py
- 奇怪的 dead lock,奇怪deadlock,未经作者许可,禁止转载!
- Python之路(第十八篇)shutil 模块、zipfile模块、configparse
- Python 有序字典(OrderedDict)与 普通字典(dict),pythonor
评论关闭