python對乒乓球比賽的分析


1、模擬單打代碼:

from random import random
def printIntro():          #打印程序介紹信息
    print("10號張穎慧進行比賽分析結果:")
    print("這個程序模擬兩個選手A和B的某種競技比賽")
    print("程序運行需要A和B的能力值(以0到1之間的小數表示)")
def getInputs():           #獲得程序運行參數
    a = eval(input("請輸入選手A的能力值(0-1): "))
    b = eval(input("請輸入選手B的能力值(0-1): "))
    n = eval(input("模擬比賽的場次: "))
    return a, b, n
def simNGames(n, probA, probB):    # 進行N場比賽
    winsA, winsB = 0, 0
    for i in range(n):
        for j in range(7):           #進行7局4勝的比賽
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
    return winsA, winsB
def gameOver(a,b):               #正常比賽結束
    return a==11 or b==11
def gameOver2(a,b):              #進行搶12比賽結束
   if abs((a-b))>=2:
       return a,b
def simOneGame(probA, probB):         #進行一場比賽
    scoreA, scoreB = 0, 0           #初始化AB的得分
    serving = "A"                 
    while not gameOver(scoreA, scoreB):     #用while循環來執行比賽
        if scoreA==10 and scoreB==10:
            return(simtwoGame2(probA,probB))
        if serving == "A":
            if random() < probA:            ##用隨機數生成勝負
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
def simtwoGame2(probA,probB):
    scoreA,scoreB=10,10
    serving = "A"
    while not gameOver2(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 = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
main()

分析1000場單打7局4勝的比賽情況結果:

 

2.團隊打5局3勝代碼:

from random import random 
def printIntro():          #打印程序介紹信息
    print("10號張穎慧進行比賽分析結果:")
    print("這個程序模擬兩個隊伍A和B的某種競技比賽")
    print("程序運行需要隊伍A和隊伍B的能力值(以0到1之間的小數表示)")
def getInputs():           #獲得程序運行參數
    a = eval(input("請輸入隊伍A的能力值(0-1): "))
    b = eval(input("請輸入隊伍B的能力值(0-1): "))
    n = eval(input("模擬比賽的場次: "))
    return a, b, n
def simNGames(n, probA, probB):    # 進行N場比賽
    winsA, winsB = 0, 0
    for i in range(n):
        for j in range(7):           #進行7局4勝的比賽
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
    return winsA, winsB
def gameOver(a,b):               #正常比賽結束
    return a==11 or b==11
def gameOver2(a,b):              #進行搶12比賽結束
    return a==12 or b==12
def simOneGame(probA, probB):         #進行一場比賽
    scoreA, scoreB = 0, 0           #初始化AB的得分
    serving = "A"                 
    while not gameOver(scoreA, scoreB):     #用while循環來執行比賽
        if scoreA==10 and scoreB==10:
            return(simtwoGame2(probA,probB))
        if serving == "A":
            if random() < probA:            ##用隨機數生成勝負
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
def simtwoGame2(probA,probB):
    scoreA,scoreB=10,10
    serving = "A"                 #假如先讓隊伍A發球
    while not gameOver2(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 = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
main()

模擬1000場打5局3勝的團隊比賽情況:

 

3、模擬排球比賽:規則7局4勝,先得24分的一方為勝,23平的話就要進行先拉2分的比賽

代碼:

from random import random
def printIntro():          #打印程序介紹信息
    print("10號張穎慧進行比賽分析結果:")
    print("這個程序模擬兩個選手A和B的某種競技比賽")
    print("程序運行需要A和B的能力值(以0到1之間的小數表示)")
def getInputs():           #獲得程序運行參數
    a = eval(input("請輸入選手A的能力值(0-1): "))
    b = eval(input("請輸入選手B的能力值(0-1): "))
    n = eval(input("模擬比賽的場次: "))
    return a, b, n
def simNGames(n, probA, probB):    # 進行N場比賽
    winsA, winsB = 0, 0
    for i in range(n):
        for j in range(7):           #進行7局4勝的比賽
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
    return winsA, winsB
def gameOver(a,b):               #正常比賽結束
    return a==24 or b==24
def gameOver2(a,b):              #進行搶12比賽結束
   if abs((a-b))>=2:
       return a,b
def simOneGame(probA, probB):         #進行一場比賽
    scoreA, scoreB = 0, 0           #初始化AB的得分
    serving = "A"                 
    while not gameOver(scoreA, scoreB):     #用while循環來執行比賽
        if scoreA==10 and scoreB==10:
            return(simtwoGame2(probA,probB))
        if serving == "A":
            if random() < probA:            ##用隨機數生成勝負
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
def simtwoGame2(probA,probB):
    scoreA,scoreB=23,23
    serving = "A"
    while not gameOver2(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 = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
main()

模擬100場比賽結果:

 

4、用pyinstaller將該程序進行封裝

 

執行情況:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM