#一、Python 测试函数
test测试的函数,没传参数的x,y,函数结果是要答应x加y的和
try:试着执行test()函数,正常就执行函数并打印x加y的和
except:否则 打印'Error'
1 def test(x,y): 2 print (x+y) 3 try: 4 test() 5 except: 6 print ('Error') 7 test(1,2)
lis_y列表解析式 就是有是一个for循环range得到64-89最终的一个列表,
test1函数求x和y的和,test1函数求x和y的乘法结果
加了判断条件x<20的一个while循环,然后y是从列表y中取值,测试加法,测试乘法的时候又加了chr的方法
chr方法返回整数i对应的ASCII字符。与ord()作用相反。
1 x = 0 2 lis_y = [i for i in range(64,90)] 3 4 def test(x,y): 5 print (x+y," "), 6 7 def test1(x,y): 8 print (x*y) 9 10 try: 11 while x < 20: 12 for y in lis_y: 13 test(x,y) 14 test1(x,chr(y)) 15 x+=1 16 except: 17 print ('Error')
二、完善球赛程序,测试你写的球赛程序,所有函数的测试结果
1 #单打的淘汰赛(七局四胜制) 2 from random import random 3 def printIntro(): #打印程序介绍信息 4 print("22号李诗然进行比赛分析结果:") 5 print("这个程序模拟两个选手A和B的某种竞技比赛") 6 print("程序运行需要A和B的能力值(以0到1之间的小数表示)") 7 def getInputs(): #获得程序运行参数 8 a = eval(input("请输入选手A的能力值(0-1): ")) 9 b = eval(input("请输入选手B的能力值(0-1): ")) 10 n = eval(input("模拟比赛的场次: ")) 11 return a, b, n 12 def simNGames(n, probA, probB): # 进行N场比赛 13 winsA, winsB = 0, 0 14 for i in range(n): 15 for j in range(7): #进行7局4胜的比赛 16 scoreA, scoreB = simOneGame(probA, probB) 17 if scoreA > scoreB: 18 winsA += 1 19 else: 20 winsB += 1 21 return winsA, winsB 22 try: 23 simNGames(0.55) 24 except: 25 print("simNGames Error") 26 27 28 def gameOver(a,b): #正常比赛结束 29 return a==11 or b==11 30 def gameOver2(a,b): #进行抢12比赛结束 31 if abs((a-b))>=2: 32 return a,b 33 def simOneGame(probA, probB): #进行一场比赛 34 scoreA, scoreB = 0, 0 #初始化AB的得分 35 serving = "A" 36 while not gameOver(scoreA, scoreB): #用while循环来执行比赛 37 if scoreA==10 and scoreB==10: 38 return(simtwoGame2(probA,probB)) 39 if serving == "A": 40 if random() < probA: ##用随机数生成胜负 41 scoreA += 1 42 else: 43 serving="B" 44 else: 45 if random() < probB: 46 scoreB += 1 47 else: 48 serving="A" 49 return scoreA, scoreB 50 51 try: 52 simOneGame(0.54) 53 except: 54 print("simNGame Error") 55 56 def simtwoGame2(probA,probB): 57 scoreA,scoreB=10,10 58 serving = "A" 59 while not gameOver2(scoreA, scoreB): 60 if serving == "A": 61 if random() < probA: 62 scoreA += 1 63 else: 64 serving="B" 65 else: 66 if random() < probB: 67 scoreB += 1 68 else: 69 serving="A" 70 return scoreA, scoreB 71 72 try: 73 simtwoGame2(0.44,0.66) 74 except: 75 print("simNGame2 Error") 76 77 def printSummary(winsA, winsB): 78 n = winsA + winsB 79 print("竞技分析开始,共模拟{}场比赛".format(n)) 80 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) 81 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) 82 def main(): 83 printIntro() 84 probA, probB, n = getInputs() 85 winsA, winsB = simNGames(n, probA, probB) 86 printSummary(winsA, winsB) 87 main()
尾号为2,访问百度网页
1 import requests 2 def getHTMLText(url): 3 try: 4 r = requests.get(url, timeout=30) 5 r.raise_for_status() 6 r.encoding = 'utf-8' 7 return r.text 8 except: 9 return "" 10 11 url = "http://www.baidu.com" 12 print(getHTMLText(url))
(此为运行的部分结果)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 </head> 6 <body> 7 8 <h1>不问花开几许,只问浅学Python</h1> 9 <h2>这是学号后两位为22同学做的第一个网页 </h2> 10 <p id="first">清风暗度,夏日绵长</p> 11 </body> 12 <table border="T"> 13 <tr> 14 <td>存在感</td> 15 <td>成就感</td> 16 </tr> 17 </html>
1 import requests 2 from bs4 import BeautifulSoup 3 allUniv = [] 4 def getHTMLText(url): 5 try: 6 r = requests.get(url, timeout=30) 7 r.raise_for_status() 8 r.encoding = 'utf-8' 9 return r.text 10 except: 11 return "" 12 def fillUnivList(soup): 13 data = soup.find_all('tr') 14 for tr in data: 15 ltd = tr.find_all('td') 16 if len(ltd)==0: 17 continue 18 singleUniv = [] 19 for td in ltd: 20 singleUniv.append(td.string) 21 allUniv.append(singleUniv) 22 def printUnivList(num): 23 print("{1:^2}{2:{0}^10}{3:{0}^6}{4:{0}^4}{5:{0}^10}".format(chr(12288),"排名","学校名称","省市","总分","年费")) 24 for i in range(num): 25 u=allUniv[i] 26 print("{1:^4}{2:{0}^10}{3:{0}^5}{4:{0}^8.1f}{5:{0}^11}".format(chr(12288),u[0],u[1],u[2],eval(u[3]),u[11])) 27 def main(): 28 url = 'http://www.zuihaodaxue.com/zuihaodaxuepaiming2016.html' 29 html = getHTMLText(url) 30 soup = BeautifulSoup(html, "html.parser") 31 fillUnivList(soup) 32 printUnivList(10) 33 main()