排球比赛的模拟赛

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

 

抄袭别人的那个:

https://www.cnblogs.com/luyingqian/p/10855064.html 

转载于:https://www.cnblogs.com/luyingqian/p/10853877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值