排球比赛的模拟赛

# -*- coding: utf-8 -*-
"""
Created on Sun May 12 21:24:19 2019

@author: history
"""

#e15.MatchAnalysis.py
#e15.MatchAnalysis.py
from random import random
def printIntro():
    print("这个程序模拟队伍A与B的排球比赛")
    print("这个程序运行需要A和B的能力值(以0-1之间的小数表示)")
def getInput():
    a=eval(input("输入队伍A的能力值:"))
    b=eval(input("输入队伍B的能力值:"))
    n=eval(input("输入模拟比赛的场次:"))
    return a,b,n
def simNGames(n,probA,probB):     #AB的能力值,以及比赛场次
    winsA,winsB=0,0       #起初的分数为0
    for i in range(n):     #循环打比赛
        scoreA,scoreB=simOneGame(probA,probB)
        if scoreA>scoreB:
            winsA +=1
        else:
            winsB +=1
    return  winsA, winsB
def gameOver(a,b):
    return a==17 or b==17
def simOneGame(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 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 main():
    printIntro()
    probA, probB,n=getInput()
    winsA,winsB=simNGames(n,probA,probB)
    printSummary(winsA,winsB)
main()

猜你喜欢

转载自www.cnblogs.com/luyingqian/p/10853877.html