模擬體育競技分析

2022-09-10 08:09:12 字數 2183 閱讀 9606

from random import random

# 列印程式介紹資訊

def printintro():

print("進行比賽分析結果:")

print("這是團體賽模擬程式:")

# 獲得程式執行引數

def getinputs():

a = eval(input("請輸入隊伍a的能力值(0-1): "))

b = eval(input("請輸入隊伍b的能力值(0-1): "))

n = eval(input("模擬比賽的場次: "))

return a, b, n

# 進行n場比賽

def simngames(n, proba, probb):

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獲勝{}場比賽,佔比".format(winsa, winsa/n))

print("隊伍b獲勝{}場比賽,佔比".format(winsb, winsb/n))

模擬體育競技分析

coding utf 8 created on tue jun 9 11 18 48 2020 author 13549 from random import random 第一階段 defprintintro print 模擬兩個選手a和b的羽毛球比賽 print 程式執行需要a和b的能力值 以0...

模擬體育競技分析

比賽規則 1.採用5局3勝制 2.前四局採用25分制,每個隊只有在贏得至少25分,且同時超過對方2分時才勝一局 3.決勝局 第五局 採用15分制,先獲得15分,且同時超過對方2分為勝 from random import random defgetinputs 獲得使用者輸入的引數 a eval i...

模擬體育競技分析

from random import random defprintintro print 36號程式設計師的程式模擬兩個選手a和b的排球比賽 print 程式需要a和b的能力值 以0到1之間的小數表示 defgetinputs a eval input 請輸入選手a的能力值 0 1 b eval ...