模拟乒乓球比赛
import random #引用random库
def sportgame():
print("Welcome to the sportgame")
print("这个程序将模拟乒乓球比赛")
#介绍程序
def InputPlayer():
player1 = eval(input("请输入运动员A能力值:"))
player2 = eval(input("请输入运动员B能力值:"))
return player1, player2
#创建InputPlayer函数:输入运动员能力值,并返回
def Gameover(scoreA,scoreB):
if scoreA == 11 and scoreB <= 9:
return scoreA
elif scoreB ==11 and scoreA <= 9:
return scoreB
elif scoreA == scoreB ==10:
if scoreA - scoreB == 2:
return scoreA
elif scoreA - scoreB == 2:
return scoreB
#Gameover函数:单局乒乓球比赛的胜利条件
def ScoringRule(probA,probB):
scoreA = 0
scoreB = 0
serving = "A"
while not Gameover(scoreA,scoreB):#调用Gameover函数,保证比赛的进行
if serving == "A":
if random.random() < probA:
scoreA += 1
else:
serving = 'B'
else:
if random.random() < probB:
scoreB += 1
else:
serving = 'A'
return scoreA,scoreB
#ScoringRule函数:记录单局乒乓球中的得分
def Singlegame(probA,probB):
winsA = winsB = 0
for i in range(7):
scoreA, scoreB = ScoringRule(probA,probB)#调用ScoringRule函数,比较得分得出胜局
if scoreA > scoreB:
winsA = winsA + 1
if winsA == 4 and winsB <=3:
print('A获胜')
else:
pass
else:
winsB = winsB + 1
if winsB == 4 and winsA <=3:
print('B获胜')
else:
pass
return winsA,winsB
#Singlegame函数:乒乓球单打比赛,采用七局四胜
def printSummary(winsA, winsB):
n = winsA + winsB
print("竞技分析开始,共模拟{}场比赛".format(7))
print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA / 7))
print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB / 7))
def main():
sportgame()
probA, probB = InputPlayer()
winsA, winsB = Singlegame(probA,probB)
printSummary(winsA, winsB)
main()
篮球
from random import random
countdown = 0
def get_inputs():
while True:
try:
foul_a_score, prob_a, prob_a3 = input('请输入 A 队的参数,用逗号隔开').split(',')
foul_bscore, prob_b, prob_b3 = input("请输入 B 队的参数,用逗号隔开").split(',')
foul_a_score = float(foul_a_score)
foul_bscore = float(foul_bscore)
prob_a = float(prob_a)
prob_b = float(prob_b)
prob_a3 = float(prob_a3)
prob_b3 = float(prob_b3)
break
except:
print('您的输入有误,请重新输入')
continue
return (foul_a_score, prob_a, prob_a3), (foul_bscore, prob_b, prob_b3)
def foul(score_a, score_b, foulAscore, foulBscore):
side = random()
if side > 0.5:
if score_a > random():
score_a += 1
else:
score_b += 1
else:
if score_b > random():
score_b += 1
else:
score_a += 1
return score_a, score_b
def simOneGame(argA, argB, ser):
global countdown
scoreA, scoreB = 0, 0
fawl = 0.3
serving = ser
i = 1
countdown = 50
foulAscore = argA[0]
probA = argA[1]
probA3 = argA[2]
foulBscore = argB[0]
probB = argB[1]
probB3 = argB[2]
while not gameOver(scoreA, scoreB):
judge = random()
if judge < fawl:
scoreA, scoreB = foul(scoreA, scoreB, foulAscore, foulBscore)
else:
if serving == 'A':
if random() < probA:
scoreA += 3 if probA3 > random() else 2
else:
scoreB += 3 if probB3 > random() else 2
else:
if random() < probB:
scoreB += 3 if probB3 > random() else 2
else:
scoreA += 3 if probA3 > random() else 2
print(scoreA, '--', scoreB)
return Winner(scoreA, scoreB)
def gameOver(scoreA, scoreB):
global countdown
if countdown == 0:
if scoreA == scoreB:
countdown += 5
return False
else:
return True
else:
countdown -= 1
return False
def switchServing(serving):
if serving == 'A':
serving = 'B'
else:
serving = 'A'
return serving
def Winner(scoreA, scoreB):
return 'A' if scoreA > scoreB else 'B'
def simOneChampion():
B = 0
A = 0
round = 1
argA, argB = get_inputs()
serving = 'A'
while True:
print('第{}节'.format(round))
r = simOneGame(argA, argB, serving)
serving = switchServing(serving)
round += 1
if r == 'A':
A += 1
else:
B += 1
if A == 3:
print('A 队胜出')
break
elif B == 3:
print('B 队胜出')
break
else:
continue
simOneChampion()
标签:scoreA,scoreB,体育,random,else,score,竞技,prob,模拟
From: https://www.cnblogs.com/88888888b/p/17933729.html