ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

错误 的模拟循环比赛

2021-11-14 12:00:38  阅读:136  来源: 互联网

标签:winsB serving 比赛 cB cA else 循环 winsA 模拟


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 printSummary(winsA, winsB):
n = winsA + winsB
print("竞技分析开始, 共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛, 占比{:0.1%}".format(winsA, winsA/n))
print("选手B获胜{}场比赛, 占比{:0.1%}".format(winsB, winsB/n))

def gameOver1(a, b):
if a>=15 and a>b:
return (a-b)==2
if b>=15 and b>a:
return (b-a)==2
else:
return False
def gameOver2(a,b):
if a>=25 and a>b:
return (a-b)==2
elif b>=25 and b>a:
return (b-a)==2
else:
return False

def simOneGame(probA, probB):
scoreA, scoreB = 0, 0
for i in range(4):
cA,cB=0,0
serving = "A"
while not gameOver2(cA,cB):
if serving == "A":
if random.random() < probA:
cA+=1
else:
cB+=1
serving = "B"
else:
if random.random() < probB:
cB += 1
else:
cA+=1
serving = "A"
if cA>cB:
scoreA +=1
else:
scoreB +=1
if scoreA==scoreB:
cA,cB=0,0
serving = "A"
while not gameOver1(cA,cB):
if serving == "A":
if random.random() < probA:
cA += 1
if cA==8 and cA>cB:
serving ='B'
else:
serving = "B"
else:
if random.random() < probB:
cB += 1
if cB==8 and cB>cA:
serving ='A'

else:
serving = "A"
if cA>cB:
scoreA +=1
else:
scoreB +=1
return scoreA,scoreB

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 main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
main()

 

 显示错误

标签:winsB,serving,比赛,cB,cA,else,循环,winsA,模拟
来源: https://www.cnblogs.com/----zcy88888/p/15551281.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有