Python之简单的IPO模式体育竞技:
1.2个运动员⽐赛
2.每次赢⼀局得⼀分
3.先到达15分结束
import random
def PrintIntro():
print('this program simulates a game between two')
print('there are two players A or B')
print('Probabliy(a number between 0 and 1)')
def getInputs():
a = eval(input('please input 1 people power'))
b = eval(input('please input 1 people power'))
n = eval(input('how many games to simulates'))
return a,b,n
#单次⽐赛函数
def simOneGameOver(procA):
s = 0
r1 = random.random()
python单例模式if procA > r1:
s = 1
return s
#游戏结束判断
def gameOver(scoreA,scoreB):
return scoreA == 15 or scoreB == 15
#多次⽐赛函数
def simNGames(a,b,n):
scoreA = 0
scoreB = 0
for i in range(n):
s = simOneGameOver(a)
if s == 1:
scoreA += 1
else:
scoreB += 1
if gameOver(scoreA, scoreB) == 1:
return scoreA, scoreB
#打印函数
def printSummary(scoreA, scoreB):
n = scoreA + scoreB
print('the result is:')
print('WinsforA:{0}({1:0.1%})'.format(scoreA, scoreA/n))
print('WinsforB:{0}({1:0.1%})'.format(scoreA, scoreB/n))
def main():
PrintIntro()
a, b, n = getInputs()
winsA, winsB = simNGames(a, b, n)
printSummary(winsA, winsB)
if __name__ == '__main__':
main()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论