• 【python】体育竞技分析:预测球队比赛成绩


    乒乓球比赛预测

    规则:再一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。单打的淘汰赛以七局四胜制,双打淘汰赛和团体赛采用五局三胜制。

     1 from random import random
     2 def printInfo():
     3     print("2019310143031")
     4     print("这个程序模拟两个选手A和B的乒乓比赛")
     5     print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
     6 def getInputs():
     7     a = eval(input("请输入选手A的能力值(0-1): "))
     8     b = eval(input("请输入选手B的能力值(0-1): "))
     9     n = eval(input("模拟比赛的场次: "))
    10     return a, b, n
    11 def simNGames(n, probA, probB):
    12     winsA, winsB = 0, 0
    13     for i in range(n):
    14         scoreA, scoreB = simOneGame(probA, probB)
    15         if scoreA > scoreB:
    16             winsA += 1
    17         else:
    18             winsB += 1
    19     return winsA, winsB
    20 def gameOver(a,b):
    21     if a>=10 and b>=10:
    22          if abs(a-b)==2:
    23              return 1
    24     elif a<10 and b<10:
    25          if a==11 or b==11:
    26              return 1
    27     else:
    28          return 0
    29 def simOneGame(probA,probB):
    30     scoreA,scoreB=0,0
    31     serving="A"
    32     while not gameOver(scoreA,scoreB):
    33          if serving=="A":
    34              if random()<probA:
    35                  scoreA+=1
    36              else:
    37                  serving="B"
    38          else:
    39              if random()<probB:
    40                  scoreB+=1
    41              else:
    42                  serving="A"
    43          return scoreA,scoreB
    44 def printSummary(winsA,winsB):
    45      n=winsA+winsB
    46      print("竞技分析开始,一共模拟{}场比赛".format(n))
    47      print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
    48      print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
    49 def main():
    50     printInfo()
    51     probA,probB,n=getInputs()
    52     winsA,winsB=simNGames(n,probA,probB)
    53     printSummary(winsA,winsB)
    54 main()
    55 python3: input("please input any key to exit!")
  • 相关阅读:
    最新的Delphi版本号对照
    SuperObject生成示例
    Why does Delphi XE7 IDE hangs and fails on out of memory exception?
    使用 TRESTClient 与 TRESTRequest 作为 HTTP Client(转)
    Delphi提取PDF文本
    (3)PyCharm中Flask工程逆向生成数据库表
    (2)PyCharm开发Flash项目之蓝图构建
    (1)PyCharm开发工具安装Flask并创建helloworld程序
    使用localStorage写一个简单的备忘录
    一个Redis实例适合存储不同应用程序的数据吗?
  • 原文地址:https://www.cnblogs.com/litchi666/p/12857011.html
Copyright © 2020-2023  润新知