Python 体模拟育比赛(排球)
import random def printIntro(): print("2019310143145") print("模拟排球比赛") print("五局三胜制") print("程序运行需要ABCD的能力值(以0到1之间的小数表示)") def getInputs(): a = eval(input("请输入选手A的能力值(0-1): ")) b = eval(input("请输入选手B的能力值(0-1): ")) c = eval(input("请输入选手C的能力值(0-1): ")) d = eval(input("请输入选手D的能力值(0-1): ")) return a,b,c,d def simOneGame(probA, probB): # 进行一场比赛 scoreA, scoreB = 0, 0 # 初始化AB的得分 serving = "A" # 首先由A发球 while not gameOver(scoreA, scoreB): #用while循环来执行比赛 if serving == "A": if random.random() < probA:# random() 方法返回随机生成的一个实数,它在[0,1)范围内。 scoreA += 1 # 用随机数来和能力值比较从而分出胜负 else: serving = "B" else: if random.random() < probB: scoreB += 1 else: serving = "A" return scoreA, scoreB def simNGames(probA, probB): #进行5场比赛 winsA, winsB = 0, 0 # 初始化AB的胜场数 for i in range(5): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsB def gameOver(a, b): #比赛结束 return (a >= 25 and a-b>1) or (b >= 25 and b-a>1) #必须大两分 def oneround(probA, probB): roundA,roundB=0,0 while not roundover(roundA,roundB): A,B=simNGames(probA, probB) if A>B: roundA+=1 else: roundB+=1 return roundA,roundB def Nround(probA,probB): winsA, winsB = 0, 0 for i in range(5): scoreA, scoreB = oneround(probA,probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsB def roundover(a,b): return a == 4 or b == 4 printIntro() probA,probB,probC,probD = getInputs() winA,winB=Nround(probA, probB) print("-------------------") print("第一轮比赛开始") print("选手A获胜{}回合".format(winA)) print("选手B获胜{}回合".format(winB)) if winA>winB: print("A胜利") E=‘A‘ else: print("B胜利") E=‘B‘ winC,winD=Nround(probC, probD) print("选手C获胜{}回合".format(winC)) print("选手D获胜{}回合".format(winD)) if winC>winD: print("C胜利") F=‘C‘ else: print("D胜利") F=‘D‘ print("---------------------") print("第二轮开始") winE=max(winA,winB) winF=max(winC,winD) winE,winF=Nround(probA, probB) print("选手{}获胜{}回合".format(E,winE)) print("选手{}获胜{}回合".format(F,winF)) if winE>winF: print("{}胜利,为冠军".format(E)) else: print("{}胜利,为冠军".format(F))
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16