Python学习八程序设计方法

体育竞技的分析

在这里插入图片描述
在这里插入图片描述

#MatchAnalysis.pyf
rom random import random
def printIntro(): 
   print("这个程序模拟两个选手A和B的某种竞技比赛")    
   print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
def getInputs():
    a = eval(input("请输入选手A的能力值(0-1): "))
    b = eval(input("请输入选手B的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: ")) 
    return a, b, n
def simNGames(n, probA, probB): 
   winsA, winsB = 0, 0  
   for i in range(n):
      scoreA, scoreB = simOneGame(probA, probB) 
      if scoreA > scoreB:  
                winsA += 1      

      else:     
             winsB += 1   

   return winsA, winsB 
def gameOver(a,b):  
   return a==15 or b==15
def simOneGame(probA, probB):
   scoreA, scoreB = 0, 0
   serving = "A"   
   while not gameOver(scoreA, scoreB):  
         if serving == "A":      
               if random() < probA:     
                          scoreA += 1      
                else:        
                        serving="B"    
          else:            
          	if random() < probB:          
                 		 scoreB += 1            
                  else:        
                          serving="A"    
   return scoreA, scoreB
def printSummary(winsA, winsB): 
   n = winsA + winsB    
   print("竞技分析开始,共模拟{}场比赛".format(n))      
   print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))    
   print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
def main():    
	printIntro()    	
 	probA, probB, n = getInputs()    
 	winsA, winsB = simNGames(n, probA, probB)    
 	printSummary(winsA, winsB)
main()

第三方库的安装方法:

pypi:Python package index

  1. pip安装
    pip install <第三方库名>
    在这里插入图片描述
    在这里插入图片描述
    pip list
    列出已经安装的第三方库
    pip download <第三方库名>:下载并不安装
  2. 集成安装方法
  3. 文件安装方法
    uci页面网站:http://www.lfd.uci.edu/~gohlke/pythonlibs/
    该方法是可以下载完整的第三方库,缺需要编译后才可以安装的情况

os库的基本介绍

  • os库是Python标准库,包含几百个函数
  • 常用路径操作、进程管理,环境参数等几类
    路径操作
    os.path子库以path为入口,用于操作和处理文件路径
    import os.path
    import os.path as op
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    os.path.size()获得文件的大小
    进程管理
    os.system(command)
  • 执行程序或者命令command
  • 在Windows系统中,返回值为cmd的调用返回信息

在这里插入图片描述
可以用指定的工具对文件进行操作
在这里插入图片描述
环境参数
在这里插入图片描述

在这里插入图片描述

第三方库自动安装脚本

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#BatchInstall.py
import os
libs ={"numpy","matplotlib","pillow","sklearn","requests",\        "jieba","beautifulsoup4","wheel","networkx","sympy",\        "pyinstaller","django","flask","werobot","pyqt5",\        "pandas","pyopengl","pypdf2","docopt","pygame"}
try:    
	for lib in libs:        
		os.system("pip3 install "+lib)    
		print("Successful")        
	except:    
		print("Failed Somehow")
发布了43 篇原创文章 · 获赞 11 · 访问量 2606

猜你喜欢

转载自blog.csdn.net/weixin_43328816/article/details/98363304