预测乒乓球比赛成绩
作者:互联网
我们通过自顶向下,从简到难的方式进行一步步地分解,设计比赛的代码
下面是通过分析乒乓球比赛所设计出的代码:
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,n
def simNGames(n,probA,probB):
winsA,winsB=0,0
for i in range(n):
scoreA,scoreB=simOneGame(probA,probB)
if scoreA>scoreB:
winsA+=1
else:
winsB+=1
return winsA,winsB
def gameOver(a,b):
return a==11 or b==11
def simOneGame(probA,probB):
scoreA,scoreB = 0,0
serving = 'A'
while not gameOver(scoreA,scoreB):
if serving == 'A':
if random()<probA:
scoreA+=1
else:
serving='B'
else:
if random()<probB:
scoreB+=1
else:
serving='A'
return scoreA,scoreB
def printSummary(winsA,winsB):
n=winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n))
print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n))
def main():
printIntro()
probA,probB,n = getInputs()
winsA,winsB = simNGames(n,probA,probB)
printSummary(winsA,winsB)
main()
尝试一下能力值差距更细微的情况
乒乓球的赛制时单打七局四胜制,双打则是五局三胜制,所以我们先用以下代码预测单打情况:
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):"))
return a,b
def simNGames(probA,probB):
winsA,winsB=0,0
for i in range(7):
scoreA,scoreB=simOneGame(probA,probB)
if scoreA>scoreB:
winsA+=1
if winsA==4:
break
else:
winsB+=1
if winsB==4:
break
return winsA,winsB
def gameOver(a,b):
return a==11 or b==11
def simOneGame(probA,probB):
scoreA,scoreB = 0,0
serving = 'A'
while not gameOver(scoreA,scoreB):
if serving == 'A':
if random()<probA:
scoreA+=1
else:
serving='B'
else:
if random()<probB:
scoreB+=1
else:
serving='A'
return scoreA,scoreB
def printSummary(winsA,winsB):
n=winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n))
print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n))
def main():
printIntro()
probA,probB = getInputs()
winsA,winsB = simNGames(probA,probB)
printSummary(winsA,winsB)
main()
试着运行一下
接下来是双打时,我们将刚刚的A/B选手抽象为A/B队伍,能力值为队伍的平均能力值
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):"))
return a,b
def simNGames(probA,probB):
winsA,winsB=0,0
for i in range(5):
scoreA,scoreB=simOneGame(probA,probB)
if scoreA>scoreB:
winsA+=1
if winsA==3:
break
else:
winsB+=1
if winsB==3:
break
return winsA,winsB
def gameOver(a,b):
return a==11 or b==11
def simOneGame(probA,probB):
scoreA,scoreB = 0,0
serving = 'A'
while not gameOver(scoreA,scoreB):
if serving == 'A':
if random()<probA:
scoreA+=1
else:
serving='B'
else:
if random()<probB:
scoreB+=1
else:
serving='A'
return scoreA,scoreB
def printSummary(winsA,winsB):
n=winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n))
print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n))
def main():
printIntro()
probA,probB = getInputs()
winsA,winsB = simNGames(probA,probB)
printSummary(winsA,winsB)
main()
运行
标签:winsB,winsA,scoreA,probA,probB,乒乓球,print,成绩,比赛 来源: https://www.cnblogs.com/komei/p/10929816.html