体育竞技分析
1 from random import random 2 def bsxx(): 3 string1="模拟体育竞技分析 模拟人:xiayiLL" 4 string2="模拟乒乓球竞技分析" 5 string3="乒乓球比赛规则如下:" 6 string4="一局比赛:" 7 string5="先得11分的一方为胜方;10平后,先多得2分的一方为胜方。" 8 string6="一场比赛:" 9 string7="单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。" 10 string8="注意:团体赛由四局单打,一局双打组成" 11 string9="顺序:一,二,四,五为单打,第三局为双打" 12 print(string1) 13 print() 14 print(string2) 15 print(string3) 16 print(string4) 17 print(string5) 18 print(string6) 19 print(string7) 20 print(string8) 21 print(string9) 22 print() 23 24 def getinputs1(): 25 print() 26 print("您选择的比赛类型是:单打淘汰赛") 27 print("为了更好的模拟比赛数据,请输入两位运动员的能力值(0-1之间)") 28 athlete1=eval(input("请输入第一位运动员能力值:")) 29 athlete2=eval(input("请输入第二位运动员能力值:")) 30 n=eval(input("请输入模拟的场次:")) 31 return athlete1,athlete2,n 32 33 def getinputs2(): 34 print() 35 print("您选择的比赛类型是:双打淘汰赛") 36 print("为了更好的模拟比赛数据,请输入四位运动员的能力值(0-1之间)") 37 print("请输入团队A的运动员能力值") 38 athlete1=eval(input("请输入第一位运动员能力值:")) 39 athlete2=eval(input("请输入第二位运动员能力值:")) 40 print("请输入团队B的运动员能力值") 41 athlete3=eval(input("请输入第三位运动员能力值:")) 42 athlete4=eval(input("请输入第四位运动员能力值:")) 43 n=eval(input("请输入模拟的场次:")) 44 aver1=(athlete1+athlete2)/2 45 aver2=(athlete3+athlete4)/2 46 return aver1,aver2,n 47 48 def getinputs3(): 49 print() 50 print("您选择的比赛类型是:团体赛") 51 print("为了更好的模拟比赛数据,请输入五局运动员的能力值(0-1之间)") 52 print("请按照出场顺序,分别输出运动员的能力值") 53 print("请输入团队A的运动员能力值") 54 athlete1=eval(input("请输入第一局运动员能力值:")) 55 athlete2=eval(input("请输入第二局运动员能力值:")) 56 athlete3=eval(input("请输入第三局运动员能力值:")) 57 athlete4=eval(input("请输入第四局运动员能力值:")) 58 athlete5=eval(input("请输入第五局运动员能力值:")) 59 print("请输入团队B的运动员能力值") 60 athlete6=eval(input("请输入第一局运动员能力值:")) 61 athlete7=eval(input("请输入第二局运动员能力值:")) 62 athlete8=eval(input("请输入第三局运动员能力值:")) 63 athlete9=eval(input("请输入第四局运动员能力值:")) 64 athlete10=eval(input("请输入第五局运动员能力值:")) 65 n=eval(input("请输入模拟的场次:")) 66 return athlete1,athlete2,athlete3,athlete4,athlete5,athlete6,athlete7,athlete8,athlete9,athlete10,n 67 68 def oneGame(N,ablity1,ablity2): 69 score1,score2=0,0 70 i=0 71 while not gameover(score1,score2): 72 if i==0: 73 for k in range(2): 74 if random()>ablity1: 75 score2+=1 76 else: 77 score1+=1 78 i+=1 79 if i==1: 80 for k in range(2): 81 if random()>ablity2: 82 score1+=1 83 else: 84 score2+=1 85 i-=1 86 return score1,score2 87 88 def allGame(ablity1,ablity2): 89 N=1 90 win1,win2=0,0 91 for i in range(7): 92 score1,score2=oneGame(N,ablity1,ablity2) 93 if score1>score2: 94 win1+=1 95 else: 96 win2+=1 97 N+=1 98 if win1==4 or win2==4: 99 break 100 return win1,win2 101 102 def Allgame(n,ablity1,ablity2): 103 wins1,wins2=0,0 104 for i in range(n): 105 score1,score2=allGame(ablity1,ablity2) 106 if score1>score2: 107 wins1+=1 108 else: 109 wins2+=1 110 return wins1,wins2 111 112 def TTgame(n,A1,A2,A3,A4,A5,B1,B2,B3,B4,B5): 113 teamA,teamB=0,0 114 A,B=0,0 115 for i in range(n): 116 a1,b1=Allgame(1000,A1,B1) 117 if a1>b1: 118 A+=1 119 else: 120 B+=1 121 a2,b2=Allgame(1000,A2,B2) 122 if a2>b2: 123 A+=1 124 else: 125 B+=1 126 a3,b3=Allgame(1000,A3,B3) 127 if a3>b3: 128 A+=1 129 else: 130 B+=1 131 if A>=3: 132 teamA+=1 133 A,B=0,0 134 elif B>=3: 135 teamB+=1 136 A,B=0,0 137 else: 138 a4,b4=Allgame(1000,A4,B4) 139 if a4>b4: 140 A+=1 141 else: 142 B+=1 143 if A>=3: 144 teamA+=1 145 A,B=0,0 146 elif B>=3: 147 teamB+=1 148 A,B=0,0 149 else: 150 a5,b5=Allgame(1000,A5,B5) 151 if a5>b5: 152 A+=1 153 else: 154 B+=1 155 return teamA,teamB 156 157 def gameover(a,b): 158 if a>=11 and (a-b)>=2: 159 return a 160 if b>=11 and (b-a)>=2: 161 return b 162 163 def alysis(n,win1,win2): 164 print("模拟竞赛分析开始,共模拟{}场比赛".format(n)) 165 print("athlete1单打获得{}场胜利,共占比{:.2f}%".format(win1,win1/n*100)) 166 print("athlete2单打获得{}场胜利,共占比{:.2f}%".format(win2,win2/n*100)) 167 print("分析完毕") 168 169 def alysis2(n,win1,win2): 170 print("模拟竞赛分析开始,共模拟{}场比赛".format(n)) 171 print("团队A双打获得{}场胜利,共占比{:.2f}%".format(win1,win1/n*100)) 172 print("团队B双打获得{}场胜利,共占比{:.2f}%".format(win2,win2/n*100)) 173 print("分析完毕") 174 175 def alysis1(n,teamA,teamB): 176 print("模拟竞赛分析开始,共模拟{}场比赛".format(n)) 177 print("团队A获得{}场胜利,共占比{:.2f}%".format(teamA,teamA/n*100)) 178 print("团队B获得{}场胜利,共占比{:.2f}%".format(teamB,teamB/n*100)) 179 print("分析完毕") 180 181 def danda(): 182 ablity1,ablity2,n=getinputs1() 183 win1,win2=Allgame(n,ablity1,ablity2) 184 print("模拟单打:") 185 alysis(n,win1,win2) 186 187 def shuangda(): 188 averablity1,averablity2,n=getinputs2() 189 win1,win2=Allgame(n,averablity1,averablity2) 190 print("模拟双打:") 191 alysis2(n,win1,win2) 192 193 def tuanti(): 194 A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,n=getinputs3() 195 teamA,teamB=TTgame(n,A1,A2,A3,A4,A5,B1,B2,B3,B4,B5) 196 alysis1(n,teamA,teamB) 197 198 def mian(): 199 bsxx() 200 x=1 201 while x==1: 202 print("请选择模拟比赛类型") 203 print("一、单打淘汰赛") 204 print("二、双打淘汰赛") 205 print("三、团体赛") 206 print("四,结束模拟分析") 207 choice1=eval(input("请输入对应比赛类型的阿拉伯数字编号:")) 208 if choice1==1: 209 danda() 210 print() 211 elif choice1==2: 212 shuangda() 213 print() 214 elif choice1==3: 215 tuanti() 216 print() 217 elif choice1==4: 218 print("模拟程序结束") 219 break 220 else: 221 print("输入错误,请重新输入") 222 print() 223 mian()

更多精彩