预测球赛比赛成绩

一、选手比赛预测
1
from random import random 2 def printIntro(): 3 print("这个程序模拟两个选手A和B的某种竞技比赛") 4 print("程序运行需要A和B的能力值(以0到1之间的小数表示)") 5 def getInputs(): 6 a = eval(input("请输入选手A的能力值(0-1): ")) 7 b = eval(input("请输入选手B的能力值(0-1): ")) 8 n = eval(input("模拟比赛的场次: ")) 9 return a, b, n 10 def simNGames(n, probA, probB): 11 winsA, winsB = 0, 0 12 for i in range(n): 13 scoreA, scoreB = simOneGame(probA, probB) 14 if scoreA > scoreB: 15 winsA += 1 16 else: 17 winsB += 1 18 return winsA, winsB 19 def gameOver(a,b): 20 return a==15 or b==15 21 def simOneGame(probA, probB): 22 scoreA, scoreB = 0, 0 23 serving = "A" 24 while not gameOver(scoreA, scoreB): 25 if serving == "A": 26 if random() < probA: 27 scoreA += 1 28 else: 29 serving="B" 30 else: 31 if random() < probB: 32 scoreB += 1 33 else: 34 serving="A" 35 return scoreA, scoreB 36 def printSummary(winsA, winsB): 37 n = winsA + winsB 38 print("竞技分析开始,共模拟{}场比赛".format(n)) 39 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) 40 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) 41 def main(): 42 printIntro() 43 probA, probB, n = getInputs() 44 winsA, winsB = simNGames(n, probA, probB) 45 printSummary(winsA, winsB) 46 main()

二、排球赛比赛预测

比赛规则:

 

  1 from random import random
  2 def printIntro():
  3     print("这个程序模拟两支排球队A和B的排球比赛")
  4     print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
  5 def getInputs():
  6     a=eval(input("请输入队伍A的能力值(0~1):"))
  7     b=eval(input("请输入队伍B的能力值(0~1):"))
  8     n=eval(input("模拟比赛的场次:"))
  9     return a,b,n
 10 def simNGames(n,probA,probB):
 11     winsA,winsB=0,0
 12     for i in range(n):
 13         scoreA,scoreB=simOneGame(probA,probB)
 14         if scoreA>scoreB:
 15             winsA +=1
 16         else:
 17             winsB +=1
 18         return winsA,winsB
 19 def gameOver(a,b):
 20         if (a>=25 and abs(a-b)>=2 )or(b>=25 and abs(a-b)>=2):
 21             return True
 22         else:
 23             return False
 24 def simOneGame(probA,probB):
 25     scoreA,scoreB=0,0
 26     serving = "A"
 27     while not gameOver(scoreA,scoreB):
 28         if serving =="A":
 29             if random()<probA:
 30                 scoreA +=1
 31             else:
 32                 serving="B"
 33         else:
 34             if random()<probB:
 35                 scoreB +=1
 36             else:
 37                 serving="A"
 38     return scoreA,scoreB
 39 def final(probA,probB):
 40      winsA,winsB=simNGames1(4,probA,probB)
 41      printSummary(winsA,winsB)
 42      if not winsA==3 or winsB==3:
 43          if winsA==winsB==2:
 44              winsA1,winsB1=simOneGame1(probA,probB)
 45              finalprintSummary(winsA,winsB)
 46      else:
 47          finalprintSummary(winsA,winsB)
 48 def simNGames1(n,probA,probB):
 49      winsA,winsB=0,0
 50      for i in range(n):
 51          scoreA,scoreB=simOneGame2(probA,probB)
 52          if winsA==3 or winsB==3:
 53              break
 54          if scoreA>scoreB:
 55              winsA+=1
 56          else:
 57              winsB+=1
 58      return winsA,winsB
 59 def simOneGame2(probA,probB):
 60      scoreA,scoreB=0,0
 61      serving="A"
 62      while not GG(scoreA,scoreB):
 63          if serving=="A":
 64              if random() < probA:
 65                  scoreA += 1
 66              else:
 67                  serving="B"
 68          else:
 69              if random() < probB:
 70                  scoreB += 1
 71              else:
 72                  serving="A"
 73      return scoreA,scoreB
 74 def simOneGame1(probA,probB):
 75     scoreA,scoreB=0,0
 76     serving="A"
 77     while not finalGameOver(scoreA,scoreB):
 78         if serving=="A":
 79             if random() < probA:
 80                 scoreA += 1
 81             else:
 82                   serving="B"
 83         else:
 84             if random() < probB:
 85                 scoreB += 1
 86             else:
 87                 serving="A"
 88                 return scoreA,scoreB
 89 def GG(a,b):
 90     return a==3 or b==3
 91 def finalGameOver(a,b):
 92      if (a==8 or b==8):
 93          if a>b:
 94              print("A队获得8分,双方交换场地")
 95          else:
 96              print("B队获得8分,双方交换场地")
 97      if (a>15 and abs(a-b)>=2 )or(b>15 and abs(a-b)>=2):
 98          return True
 99      else:
100          return False
101 def finalprintSummary(winsA,winsB):
102      n=winsA+winsB
103      if n>=4:
104          print("进行最终决赛")
105          if winsA>winsB:
106              print("最终决赛由A获胜")
107          else:
108              print("最终决赛由B获胜")
109      else:
110             if winsA>winsB:
111                 print("最终决赛由A获胜")
112             else:
113                 print("最终决赛由B获胜")
114 def printSummary(winsA,winsB):
115         n=winsA+winsB
116         print("竞技分析开始,共模拟{}场比赛".format(n))
117         print("队伍A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
118         print("队伍B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
119 def main():
120         printIntro()
121         probA,probB,n=getInputs()
122         winsA,winsB=simNGames(n,probA,probB)
123         printSummary(winsA,winsB)
124         final(probA,probB)
125 main()

posted @ 2020-04-22 12:43  WendyWW  阅读(261)  评论(0)    收藏  举报