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

Python 之 简单的IPO模式_第1张图片

你可能感兴趣的:(Python 之 简单的IPO模式)