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()
    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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值