排球比赛模拟
作者:互联网
from random import random def printIntro(): print("44号运用这个程序模拟A,B两个选手的比赛:\n") print("这个程序的实现需要知道A,B两个选手的能力值,该值为0-1之间的小数:\n") def getInputs(): probA = eval(input("请输入A的能力值:")) probB = eval(input("请输入B的能力值:")) n=int(input("请输入比赛场次:")) return probA,probB,n def printSummary(winsA,winsB): print("预计A会赢得{}场比赛".format(winsA)) print("预计44号B会赢得{}场比赛".format(winsB)) def simNGames(probA,probB,n): 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 simOneGame(probA,probB): scoreA,scoreB=0,0 serve='A' while not onegameOver(scoreA,scoreB): if (serve=='A'): if(random()<=probA): scoreA += 1 else: serve='B' if (serve=='B'): if(random()<=probB): scoreB += 1 else: serve='A' return scoreA,scoreB def onegameOver(a,b): return a==15 or b==15 probA,probB,n=getInputs() winsA,winsB=simNGames(probA,probB,n) printSummary(winsA,winsB)
标签:winsB,winsA,probB,probA,def,排球比赛,print,模拟 来源: https://www.cnblogs.com/zjx15975136073/p/15551282.html