zoukankan      html  css  js  c++  java
  • 预测球队比赛结果及利用pyinstaller打包文件

    一、预测乒乓球球队比赛成绩

    1、乒乓球比赛规则

    一局比赛:在一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。

    一场比赛:单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。

    2、程序总体框架:

    (1)打印程序的介绍信息式

    (2)获得程序运行参数:probA 、probB 、n

    (3)利用球员A和B的能力值,模拟n局比赛

    (4)输出球员A和B获胜比赛的场次及概率

    3、代码如下

     1 from random import random
     2 def printInfo():
     3     print("2019310143011")
     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!")

     特别提醒:可以在要打包的py文件主函数添加input,不然打包后的.exe文件执行后可能会闪退

    最后一行可以添加一下代码(区分python2和python3)

    1 python2: raw_input("please input any key to exit!")
    2 python3: input("please input any key to exit!")

    4、运行结果如下:

    二、利用pyinstaller打包文件

    1、先打开cmd安装pyinstaller

    2、打包:需要在要打包的文件内打开命令行,输入pyinstaller -F 你的文件名 

    3、打包成功后找到dist文件里面就是生成后的可执行文件啦

     4、点击运行,试试吧

    三、采用篮球比赛规则模拟比赛

    1、代码

    import random
    def printInfo():
        print("by 2019310143011")
        print("这个程序模拟两支球队A和B的篮球比赛")
        print("程序运行需要球队A和B的能力值(以0到1之间的小数表示)")
    def getInputis():
        a = eval(input("请输入球队A的能力值(0-1): "))
        b = eval(input("请输入球队B的能力值(0-1): "))
        n = eval(input("请输入模拟场次:"))
        return a, b, n
    def simOneGame(n,probA, probB):
        scoreA, scoreB = 0, 0
        serving = "A"
        a=0
        while (a<=n) or (a>n) and (scoreA==scoreB):
            if serving == "A":
                if random.random() < probA:
                    scoreA += 1
                else:
                    serving="B"
            else:
                if random.random() < probB:
                    scoreB += 1
                else:
                    serving="A"
            a+=1
        return scoreA, scoreB
    def printSummary(n, scoreA, scoreB):
        print("竞技分析开始,共模拟{}场比赛".format(n))
        print("球队A获得{}分".format(scoreA))
        print("球队B获得{}分".format(scoreB))
        if scoreA>scoreB:
            char='A'
        else:
            char='B'
        print("获胜方是球队{}".format(char))
    def main():
        printInfo()
        probA, probB, n = getInputs()
        scoreA, scoreB = simOneGame(n,probA, probB)
        printSummary(n, scoreA, scoreB)
    main()
    input("input any keyword")

    2、结果如下

  • 相关阅读:
    mybatis返回map类型数据空值字段不显示(三种解决方法)
    linux各种资源查看
    ssh代理
    python 自定义ping编写
    python 备忘(协程,多进程)
    python 控制电源模块for循环写法
    XPath 语法
    python 自定义去掉特殊字符串
    ssh证书登陆vpc/并且反向代理
    cmder设置
  • 原文地址:https://www.cnblogs.com/qinlai/p/12730831.html
Copyright © 2011-2022 走看看