赞
踩
注明:本系列课程专为全国计算机等级考试二级 Python 语言程序设计考试服务
目录
“体育竞技分析”实例
• 步骤1: 打印程序的介绍性信息;
• 步骤2:获得程序运行需要的参数:probA, probB, n;
• 步骤3:利用球员A和B的能力值probA和probB,模拟n次比赛;
• 步骤4:输出球员A和B获胜比赛的场次及概率。
- def main():
- printIntro()
- def main():
- printIntro()
- probA, probB, n = getInputs()
- def main():
- printIntro()
- probA, probB, n = getInputs()
- winsA, winsB = simNGames(n, probA, probB)
- def main():
- printIntro()
- probA, probB, n = getInputs()
- winsA, winsB = simNGames(n, probA, probB)
- printSummary(winsA, winsB)
- 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 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 gameOver(a,b):
- return a==15 or b==15
- 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))
- >>>
- 这个程序模拟两个选手A和B的某种竞技比赛
- 程序运行需要A和B的能力值(以0到1之间的小数表示)
- 请输入选手A的能力值(0-1): 0.45
- 请输入选手B的能力值(0-1): 0.5
- 模拟比赛的场次: 1000
- 竞技分析开始,共模拟1000场比赛
- 选手A获胜371场比赛,占比37.1%
- 选手B获胜629场比赛,占比62.9%
整个过程可以概括为四个步骤:
import <源文件名称>
- >>>import MatchAnalysis
- >>>MatchAnalysis.gameOver(15, 10)
- True
- >>>MatchAnalysis.gameOver(10, 1)
- False
- >>>import e151MatchAnalysis
- >>>e151MatchAnalysis.simOneGame(.45, .5)
- (9, 15)
- >>>e151MatchAnalysis.simOneGame(.45, .5)
- (15, 13)
函数名称 | 函数说明 |
abs(x) | x的绝对值,如果x是复数,返回复数的模 |
all(x) | 组合类型变量x中所有元素都为真时返回True,否则返回False;若x为空,返回True |
any(x) | 组合类型变量x中任一元素都为真时返回True,否则返回False;若x为空,返回False |
bin(x) | 将整数x转换为等值的二进制字符串,bin(1010)的结果是'0b1111110010' |
bool(x) | 将x转换为Boolean类型,即True或False,bool('') 的结果是False |
chr(i) | 返回Unicode为i的字符,chr(9996)的结果是'✌' |
complex(r,i) | 创建一个复数 r + i*1j,其中i可以省略,complex(10,10)的结果是10+10j |
dict() | 创建字典类型,dict()的结果是一个空字典{} |
divmod(a,b) | 返回a和b的商及余数,divmod(10,3)结果是一个(3,1) |
eval(s) | 计算字符串s作为Python表达式的值,eval('1+99')的结果是100 |
exec(s) | 计算字符串s作为Python语句的值,exec('a = 1+999')运行后,变量a的值为1000 |
float(x) | 将x转换成浮点数,float(1010)的结果是1010.0 |
hex(x) | 将整数转换为16进制字符串,hex(1010)的结果是'0x3f2' |
input(s) | 获取用户输入,其中s是字符串,作为提示信息,s可选 |
int(x) | 将x转换成整数,int(9.9)的结果是9 |
list(x) | 创建或将变量x转换成一个列表类型,list({10,9,8})的结果是[8,9,10] |
max(a1,a2,…) | 返回参数的最大值,max(1,2,3,4,5)的结果是5 |
min(a1,a2,…) | 返回参数的最小值,min(1,2,3,4,5)的结果是1 |
oct(x) | 将整数x转换成等值的八进制字符串形式,oct(1010)的结果是'0o1762' |
open(fname, m) | 打开文件,包括文本方式和二进制方式等,其中,m部分可以省略,默认是以文本可读形式打开 |
ord(c) | 返回一个字符的Unicode编码值,ord('字')的结果是23383 |
pow(x,y) | 返回x的y次幂,pow(2,pow(2,2))的结果是16 |
print(x) | 打印变量或字符串x,print()的end参数用来表示输出的结尾字符 |
range(a,b,s) | 从a到b(不含)以s为步长产生一个序列,list(range(1,10,3))的结果是[1, 4, 7] |
reversed(r) | 返回组合类型r的逆序迭代形式,for i in reversed([1,2,3])将逆序遍历列表 |
round(n) | 四舍五入方式计算n,round(10.6)的结果是11 |
set(x) | 将组合数据类型x转换成集合类型,set([1,1,1,1])的结果是{1} |
sorted(x) | 对组合数据类型x进行排序,默认从小到大,sorted([1,3,5,2,4])的结果是[1,2,3,4,5] |
str(x) | 将x转换为等值的字符串类型,str(0x1010)的结果是'4112' |
sum(x) | 对组合数据类型x计算求和结果,sum([1,3,5,2,4])的结果是15 |
type(x) | 返回变量x的数据类型,type({1:2})的结果是<class 'dict'> |
• 步骤1: 读取保存在本地的html文件;
• 步骤2:解析并提取其中的图片链接;
• 步骤3:输出提取结果到屏幕;
• 步骤4:保存提取结果为文件。
- def main():
- inputfile = 'nationalgeographic.html'
- outputfile = 'nationalgeographic-urls.txt'
- htmlLines = getHTMLlines(inputfile)
- imageUrls = extractImageUrls(htmlLines)
- showResults(imageUrls)
- saveResults(outputfile, imageUrls)
- def getHTMLlines(htmlpath):
- f = open(htmlpath, "r", encoding='utf-8')
- ls = f.readlines()
- f.close()
- return ls
<img title="photo story"
src="http://image.nationalgeographic.com.cn/2018/0122/20
180122042251164.jpg" width="968px" />
- def extractImageUrls(htmllist):
- urls = []
- for line in htmllist:
- if 'img' in line:
- url = line.split('src=')[-1].split('"')[1]
- if 'http' in url:
- urls.append(url)
- return urls
- def showResults(urls):
- count = 0
- for url in urls:
- print('第{:2}个URL:{}'.format(count, url))
- count += 1
- def saveResults(filepath, urls):
- f = open(filepath, "w")
- for url in urls:
- f.write(url+"\n")
- f.close()
- def getHTMLlines(htmlpath):
- f = open(htmlpath, "r", encoding='utf-8')
- ls = f.readlines()
- f.close()
- return ls
-
- def extractImageUrls(htmllist):
- urls = []
- for line in htmllist:
- if 'img' in line:
- url = line.split('src=')[-1].split('"')[1]
- if 'http' in url:
- urls.append(url)
- return urls
-
- def showResults(urls):
- count = 0
- for url in urls:
- print('第{:2}个URL:{}'.format(count, url))
- count += 1
-
- def saveResults(filepath, urls):
- f = open(filepath, "w")
- for url in urls:
- f.write(url+"\n")
- f.close()
-
- def main():
- inputfile = 'nationalgeographic.html'
- outputfile = 'nationalgeographic-urls.txt'
- htmlLines = getHTMLlines(inputfile)
- imageUrls = extractImageUrls(htmlLines)
- showResults(imageUrls)
- saveResults(outputfile, imageUrls)
-
- main()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- >>>
-
- 第 0个URL:http://image.nationalgeographic.com.cn/2018/0122/20180122042251164.jpg
- 第 1个URL:http://image.nationalgeographic.com.cn/2018/0122/20180122120753804.jpg
- 第 2个URL:http://image.nationalgeographic.com.cn/2018/0122/20180122102058707.jpg
- 第 3个URL:http://image.nationalgeographic.com.cn/2018/0118/20180118124326995.jpg
- 第 4个URL:http://image.nationalgeographic.com.cn/2018/0116/20180116112407593.jpg
- 第 5个URL:http://image.nationalgeographic.com.cn/2018/0122/20180122035438691.jpg
- 第 6个URL:http://image.nationalgeographic.com.cn/2018/0118/20180118040311659.jpg
- 第 7个URL:http://image.nationalgeographic.com.cn/2018/0117/20180117022633730.jpg
- 第 8个URL:http://image.nationalgeographic.com.cn/2018/0115/20180115024334826.jpg
- 第 9个URL:http://image.nationalgeographic.com.cn/2018/0112/20180112025424524.png
- 第10个URL:http://image.nationalgeographic.com.cn/2018/0122/20180122105741158.jpg
- #下面省去40个URL
本章主要讲解程序设计方法学,包括计算思维、自顶向下设计和自底向上执行等,进一步本章介绍了计算生态的概念及Python标准库和第三方库的划分。通过Web页面元素提取的实例帮助读者理解自顶向下设计的基本方法。
从最基本的IPO到自顶向下设计,是否感受到了函数式编程的优势?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。