from random import random
print('学号后两位:47')
print('22信计1晁丽
,2022310143047')
def first():
print("这个程序模拟两个选手A和B的羽毛球竞技比赛")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
def second():
a = float(input("请输入选手A的能力值(0-1): "))
b = float(input("请输入选手B的能力值(0-1): "))
n = int(input("模拟比赛的场次: "))
return a, b, n
def third(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 gameOver(scoreA, scoreB):
if scoreA == 29 and scoreB == 29:
return False
elif scoreA >= 30 or scoreB >= 30:
return True
elif (scoreA >= 21 or scoreB >= 21)and abs(scoreA - scoreB) >= 2:
return True
else:
return False
def final(winsA, winsB, n):
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
def simulateMatches(probA, probB, n):
winsA, winsB = 0, 0
for i in range(n):
scoreA, scoreB = third(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def main():
first()
probA, probB, n = second()
winsA, winsB = simulateMatches(probA, probB, n)
final(winsA, winsB, n)
main()
标签:winsB,winsA,羽毛球,scoreA,scoreB,比赛规则,print,return From: https://www.cnblogs.com/chaolili/p/17933798.html