#比賽規則:
#1. 採用5局3勝制
#2. 前四局採用25分制,每個隊只有在贏得至少25分,且同時超過對方2分時才勝一局
#3. 決勝局(第五局)採用15分制,先獲得15分,且同時超過對方2分為勝
from random import
random
defgetinputs():
#獲得使用者輸入的引數
a = eval(input("
請輸入隊伍a的能力值(0->1):"))
b = eval(input("
請輸入隊伍b的能力值(0->1):"))
n = eval(input("
請輸入比賽次數:"))
return
a, b, n
defngames(n, proba, probb):
#模擬n場比賽
wina, winb =0, 0
for _ in
range(n):
scorea, scoreb =onegame(proba, probb)
if scorea >scoreb:
wina += 1
else
: winb += 1
return
wina, winb
defonegame(proba, probb):
#模擬一場比賽,包括五局
scorea, scoreb, n =0, 0, 0
serving = 'a'
while
notgameover(n, scorea, scoreb):
if serving == 'a'
:
if random() >proba:
scoreb += 1serving = 'b'
else
: scorea += 1
if serving == 'b'
:
if random() >probb:
scorea += 1serving = 'a'
else
: scoreb += 1n += 1
return
scorea, scoreb
defgameover(n, a, b):
#定義贏得一局的條件
#n: 當前局次(第五局為決勝局)
if n <= 4:
return (a>=25 and b>=25 and abs(a-b)>=2)
else
:
return (a>=15 and b>=15 and abs(a-b)>=2)
defprintresult(n, wina, winb):
print("
競技分析開始,共模擬{}場比賽
".format(n))
print("
隊伍a獲勝{}場比賽,佔比
".format(wina,wina/n))
print("
隊伍b獲勝{}場比賽,佔比
".format(winb,winb/n))
if__name__ == "
__main__":
proba, probb, n =getinputs()
wina, winb =ngames(n, proba, probb)
printresult(n, wina, winb)
模擬體育競技分析
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...
模擬體育競技分析
from random import random defprintintro print 36號程式設計師的程式模擬兩個選手a和b的排球比賽 print 程式需要a和b的能力值 以0到1之間的小數表示 defgetinputs a eval input 請輸入選手a的能力值 0 1 b eval ...
模擬體育競技分析
from random import random 列印程式介紹資訊 def printintro print 進行比賽分析結果 print 這是團體賽模擬程式 獲得程式執行引數 def getinputs a eval input 請輸入隊伍a的能力值 0 1 b eval input 請輸入隊伍...