python模拟体育竞技分析

发布时间 2023-12-24 22:29:14作者: 事事顺心ouo

(1)体育竞技模拟分析:

采用乒乓球比赛规则

‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬a.一局比赛‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬在一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬b.一场比赛:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。

程序要求:

1.打印程序的介绍性信息式(要求包括你们学号后两位信息)‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬2.输入I(input):两个球员(球队)的能力值,模拟比赛的次数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬3.处理P(process):模拟比赛过程‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬4.输出O(output):两个球员(球队)获胜的概率(输出谁做的结果截图),‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬5. 运用pyinstaller打包可以执行的文件。

<1>. 单人赛模拟程序:

 1 from random import random
 2 
 3 
 4 # 打印程序介绍信息
 5 def printIntro():
 6     print("22号")
 7     print("进行比赛分析结果:")
 8     print("这是团体赛模拟程序:")
 9 
10 
11 # 获得程序运行参数
12 def getInputs():
13     a = eval(input("请输入队伍A的能力值(0-1): "))
14     b = eval(input("请输入队伍B的能力值(0-1): "))
15     n = eval(input("模拟比赛的场次: "))
16     return a, b, n
17 
18 
19 # 进行N场比赛
20 def simNGames(n, probA, probB):
21     winsA, winsB = 0, 0
22     for i in range(n):
23         for j in range(7):           # 进行7局4胜的比赛
24             scoreA, scoreB = simOneGame(probA, probB)
25             if scoreA > scoreB:
26                 winsA += 1
27             else:
28                 winsB += 1
29     return winsA, winsB
30 
31 
32 # 正常比赛结束
33 def gameOver(a, b):
34     return a == 11 or b == 11
35 
36 
37 def gameOver2(a, b):              # 进行抢12比赛结束
38     return a == 12 or b == 12
39 
40 
41 # 进行一场比赛
42 def simOneGame(probA, probB):
43     scoreA, scoreB = 0, 0             # 初始化AB的得分
44     serving = "A"
45     while not gameOver(scoreA, scoreB):     # 用while循环来执行比赛
46         if scoreA == 10 and scoreB == 10:
47             return(simtwoGame2(probA,probB))
48         if serving == "A":
49             if random() < probA:            # 用随机数生成胜负
50                 scoreA += 1
51             else:
52                 serving = "B"
53         else:
54             if random() < probB:
55                 scoreB += 1
56             else:
57                 serving="A"
58     return scoreA, scoreB
59 
60 
61 def simtwoGame2(probA,probB):
62     scoreA, scoreB=10, 10
63     serving = "A"                 # 假如先让队伍A发球
64     while not gameOver2(scoreA, scoreB):
65         if serving == "A":
66             if random() < probA:
67                 scoreA += 1
68             else:
69                 serving = "B"
70         else:
71             if random() < probB:
72                 scoreB += 1
73             else:
74                 serving = "A"
75     return scoreA, scoreB
76 
77 
78 def printSummary(winsA, winsB):
79     n = winsA + winsB
80     print("竞技分析开始,共模拟{}场比赛".format(n))
81     print("队伍A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
82     print("队伍B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
83 
84 
85 def main():
86     printIntro()
87     probA, probB, n = getInputs()
88     winsA, winsB = simNGames(n, probA, probB)
89     printSummary(winsA, winsB)
90 
91 
92 main()

运行结果:

 

 <2>. 团体赛模拟程序:

 1 from random import random
 2 
 3 
 4 # 打印程序介绍信息
 5 def printIntro():
 6     print("22号")
 7     print("进行比赛分析结果:")
 8     print("这是团体赛模拟程序:")
 9 
10 
11 # 获得程序运行参数
12 def getInputs():
13     a = eval(input("请输入队伍A的能力值(0-1): "))
14     b = eval(input("请输入队伍B的能力值(0-1): "))
15     n = eval(input("模拟比赛的场次: "))
16     return a, b, n
17 
18 
19 # 进行N场比赛
20 def simNGames(n, probA, probB):
21     winsA, winsB = 0, 0
22     for i in range(n):
23         for j in range(7):           # 进行7局4胜的比赛
24             scoreA, scoreB = simOneGame(probA, probB)
25             if scoreA > scoreB:
26                 winsA += 1
27             else:
28                 winsB += 1
29     return winsA, winsB
30 
31 
32 # 正常比赛结束
33 def gameOver(a, b):
34     return a == 11 or b == 11
35 
36 
37 def gameOver2(a, b):              # 进行抢12比赛结束
38     return a == 12 or b == 12
39 
40 
41 # 进行一场比赛
42 def simOneGame(probA, probB):
43     scoreA, scoreB = 0, 0             # 初始化AB的得分
44     serving = "A"
45     while not gameOver(scoreA, scoreB):     # 用while循环来执行比赛
46         if scoreA == 10 and scoreB == 10:
47             return(simtwoGame2(probA,probB))
48         if serving == "A":
49             if random() < probA:            # 用随机数生成胜负
50                 scoreA += 1
51             else:
52                 serving = "B"
53         else:
54             if random() < probB:
55                 scoreB += 1
56             else:
57                 serving="A"
58     return scoreA, scoreB
59 
60 
61 def simtwoGame2(probA,probB):
62     scoreA, scoreB=10, 10
63     serving = "A"                 # 假如先让队伍A发球
64     while not gameOver2(scoreA, scoreB):
65         if serving == "A":
66             if random() < probA:
67                 scoreA += 1
68             else:
69                 serving = "B"
70         else:
71             if random() < probB:
72                 scoreB += 1
73             else:
74                 serving = "A"
75     return scoreA, scoreB
76 
77 
78 def printSummary(winsA, winsB):
79     n = winsA + winsB
80     print("竞技分析开始,共模拟{}场比赛".format(n))
81     print("队伍A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
82     print("队伍B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
83 
84 
85 def main():
86     printIntro()
87     probA, probB, n = getInputs()
88     winsA, winsB = simNGames(n, probA, probB)
89     printSummary(winsA, winsB)
90 
91 
92 main()

运行结果:

 

 <3>.运用pyinstaller打包可以执行的文件

 

 EXE如下:

 

 

(2)采用篮球或者足球规则模拟比赛,分析体育竞技规则,模拟比赛。(良好),基本要求同上。

采用篮球比赛规则:

 1 # -*- coding: utf-8 -*-
 2 from random import random
 3 def printIntro():
 4     print("22号")
 5     print("这个程序模拟两个队伍A和B的篮球比赛")
 6     print("程序运行需要A和B队伍的平均能力值(以0到1之间的小数表示)")
 7 def getInput():
 8     probA=eval(input("请输入队伍A的平均能力值(0-1):"))
 9     probB=eval(input("请输入队伍B的平均能力值(0-1):"))
10     n=eval(input("模拟比赛场次:"))
11     return probA,probB,n
12 def Games(n,probA,probB):            #全部比赛场次的各胜方总数分析
13    A,B=0,0
14    for i in range(n):
15         a=fourGames(probA,probB)
16         if a=="A":
17             A+=1
18         else:
19            B+=1
20    return A,B
21 def fourGames(probA,probB):          #一场比赛的胜利方判断
22    scoreA,scoreB=0,0
23    for i in range(4):
24         scoreA,scoreB=onejieGame(probA,probB)
25    if scoreA>scoreB:
26         return 'A'
27    return 'B'
28 def onejieGame(probA,probB):         #一小节比赛的比分总结
29     scoreA,scoreB=0,0
30     serving="A"
31     while not gameOver(scoreA,scoreB):
32         a=random()
33         if serving=="A":
34            if a<probA:
35                scoreA+=2
36            else:
37                serving="B"
38         else:
39             if a<probB:
40                 scoreB+=2
41             else:
42                 serving="A"
43     return scoreA,scoreB
44 def gameOver(a,b):
45   return a==16 or b==16
46 def printsummary(A,B):
47     n=A+B
48     print("竞技开始分析,共模拟了{}场比赛".format(n))
49     print("选手A获胜{}场比赛,占比{:0.1%}".format(A,A/n))
50     print("选手B获胜{}场比赛,占比{:0.1%}".format(B,B/n))
51 def main():
52     printIntro()
53     probA,probB,n=getInput()
54     A,B=Games(n,probA,probB)
55     printsummary(A,B)
56 main()

运行结果:

 

 

 

(3)采用乒乓球、羽毛球、篮球、足球等规则中的一种。同时采用循环赛或者晋级赛形式,模拟分析4个队及以上体育竞技,并输出排名。建议可以采用面向对象方法或者改动上面程序。

 1 from random import random
 2 def printIntro():
 3     print("这个程序模拟四支队伍A,B,C,D的篮球比赛,赛制采取晋级制")
 4     print("程序运行需要A,B,C,D队伍的平均能力值(以0到1之间的小数表示)")
 5 def getInput():
 6     probA=eval(input("请输入队伍A的平均能力值(0-1):"))
 7     probB=eval(input("请输入另一支队伍B的平均能力值(0-1):"))
 8     print("\n")
 9     return probA,probB
10 def fourGames(x,y):          #一场比赛的胜利方判断
11     scoreA,scoreB=0,0
12     for i in range(4):
13         scoreA,scoreB=onejieGame(x,y)
14     if scoreA>scoreB:
15         return 'A'
16     return 'B'
17 def onejieGame(x,y):         #一小节比赛的比分总结
18     scoreA,scoreB=0,0
19     serving="A"
20     while not gameOver(scoreA,scoreB):
21         a=random()
22         if serving=="A":
23             if a<x:
24                 scoreA+=2
25             else:
26                 serving="B"
27         else:
28             if a<y:
29                 scoreB+=2
30             else:
31                 serving="A"
32     return scoreA,scoreB
33 
34 def gameOver(a,b):
35     return a==16 or b==16
36 def printmean(c):
37     print("竞技开始:")
38     print("经过激烈的比赛,最终队伍{}获得了比赛".format(c))
39     print("恭喜{}队伍晋级!".format(c))
40     print("\n")
41 printIntro()
42 x,y=getInput()
43 c=fourGames(x,y)
44 printmean(c)
45 print("请再次输入其余两支队伍的平均能力值")
46 x,y=getInput()
47 d=fourGames(x,y)
48 printmean(d)
49 print("晋级双方是{}和{}".format(c,d))
50 print("\n")
51 print("请输入两支晋级队伍平均能力值")
52 x,y=getInput()
53 e=fourGames(x,y)
54 print("恭喜{}获得本次比赛的总冠军!".format(e))

运行结果: