收藏 分销(赏)

Python程序设计基础课后练习题答案1-13章全.docx

上传人:快乐****生活 文档编号:3157992 上传时间:2024-06-21 格式:DOCX 页数:34 大小:44.71KB
下载 相关 举报
Python程序设计基础课后练习题答案1-13章全.docx_第1页
第1页 / 共34页
Python程序设计基础课后练习题答案1-13章全.docx_第2页
第2页 / 共34页
Python程序设计基础课后练习题答案1-13章全.docx_第3页
第3页 / 共34页
Python程序设计基础课后练习题答案1-13章全.docx_第4页
第4页 / 共34页
Python程序设计基础课后练习题答案1-13章全.docx_第5页
第5页 / 共34页
点击查看更多>>
资源描述

1、第一章判断题:1-4: 第二章判断题:1-5: 6-10: 11-15: 添加代码题:1我们都知道,下面的代码:print “I like writing in Python.”print “It is so much fun.”执行后,运行结果为:I like writing in Python.It is so much fun.你能只用一行代码实现上述效果吗?第3章 习题参考答案:一、 选择题ABABB二、 简答题1break或continue语句用来提前跳出循环,即循环条件没有满足False时或者序列还没被完全递归完,也会停止执行循环语句。其中,continue 语句用于跳出本次循环,

2、而break用于跳出整个循环。该程序段的功能是检查用户输入的用户名及密码是否正确,输入正确则显示“登录成功”;输入错误则由用户重新输入,但输入错误次数超过3次则不允许再次输入,直接显示“登录失败”。break在此程序段中的作用是当输入用户名和密码正确时或输入错误次数超过3次时直接结束循环。2Python语句代码缩进的书写原则:在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组。这意味着同一层次的语句必须有相同的缩进。有相同的缩进的代码表示这些代码属于同一代码块。代码段1和代码段2的区别在于“print(sum)”这个语句的位置。在代码段1中,“print(sum)

3、”和“for i in range(10):”在同一层次,表示它们是并列的语句,“print(sum)”不在循环体内,所以循环结束后才输出sum的值;而在代码段2中,“print(sum)”在循环体内,表示每循环一次都会输出一次sum的值。3错误1:循环嵌套代码的缩进错误2:range(1,4)只是包括1,2,3,不包括4。所以要改成range(1,5)错误3:if( i!=j!=k)的写法不对。改为if( i!=j and j!=k)4输出结果1355. bonus1 = 100000 * 0.1bonus2 = bonus1 + 100000 * 0.500075bonus4 = bonu

4、s2 + 200000 * 0.5bonus6 = bonus4 + 200000 * 0.3bonus10 = bonus6 + 400000 * 0.15i = int(raw_input(input gain:n)if i = 100000: bonus = i * 0.1elif i = 200000: bonus = bonus1 + (i - 100000) * 0.075elif i = 400000: bonus = bonus2 + (i - 200000) * 0.05elif i = 600000: bonus = bonus4 + (i - 400000) * 0.03

5、elif i =90: print(A)else: 60=score90: print(B)else: print(C)7. str=input(enter a sentence:)for char in str: if 65=ord(char)=90 or 97=ord(char)=122: print(英文字母) elif ord(char)=32: print(空格) elif 48=ord(char)0: print(num%10,end=) i+=1 num=num/10print(n这个数是%d位数%i)10. for i in range(100,1000): if i %7=0

6、 and i/10%10=2: print (i)11.j=0for i in range(2000,2501): if i %400=0 or i%4=0 and i%100!=0: j+=1 print(i,end= ) if j %8=0: print()12. i=7while True: if i%2=1 and i%3=2 and i%4=3 and i%5=4 and i%6=5 and i%7=0: print(i) break i+=113. total=13i=0while total=26: total=total*(1+0.008) i+=1print(i)14. nu

7、m=int(input(please enter num:)flag=Truefor i=2 to num-1: if num%i=0: flag=Falseif flag: print(%d是素数%num)else: print(%d不是素数%num)15. k=0for i in range(1,1001): flag=True for j in range(2,i): if i%j=0: flag=False if flag: k+=1 print(i,end= ) if k%10=0: print() k=116. a=int(input(enter a:)b=int(input(en

8、ter b:)c=int(input(enter c:)if ac: if bc: print(a,b,c) else: print(a,c,b)else: print(c,a,b)三、 实训题1. 求最大公约数代码参考书上例3.6。求出最大公约数后即可相应求出最小公倍数。2. 求1!+2!+3!+20!参考代码:sum=0for i in range(1,21): t=1for j in range(1,i+1):t*=jsum+=tprint(sum)3. 输出斐波那契数列的前20项参考代码:f1=1f2=1for i in range(1,21):print(f1,f2) f1+=f2

9、f2+=f14. 编程找出1000之内的所有完数,并输出其因子。参考代码:l = for n in range (1,1001): for a in range (1,n): if n%a =0: l.append(a) if sum(l)=n: print (l) print (n) l = 5. 输出九九乘法表参考代码:for x in range(1,10): for y in range(1,x+1): r=x*y print (%d * %d = %-2d %(y,x,r), ,end=)#%d格式化成整数,- 代表左对齐,数字代表占位。 print (end=n)#这一句代表,每次

10、遍历完一个周期换行,并下一次遍历的结果将从该行输出。如果是print()将从下一行开始输出。6. 输入一系列数字,并求和与求平均数。sum=0.0i=0num=int(input(请输入数字,以0结束)while num!=0: sum+=num num=int(input(请输入数字,以0结束) i+=1average=sum/iprint(加起来总数为%d,平均数%f%(sum,average)第4章课后练习答案:一、填空题:1h/e/l/l/o/ /w/o/r/l/d/! 2.回车换行 3.False 4.(1) str-1:-1 (2) str.upper() (3)str5:12 (

11、4)str:2 (5) /.join(list(str) (6)www.sina.www.gdpu.www.good.www.tianya.replace(www,万维网) 5.c:test.htm 6. 1 7. HELLO WORLD 8. True 9. 123456 10.True二、简答题:1.假设有一段英文,其中有单词中间的字母“i”误写为“l”,请编写程序进行纠正。x = i am a teacher,i am man, and i am 38 years old.I am not a businessman.x = x.replace(i ,I )print(x)2.有一段英文

12、文本,其中有单词连续重复了2次,编写程序检查重复的单词并只保留一个。例如文本内容为“This is is a desk.”,程序输出为“This is a desk.”x=Thisisaadesk.pattern=pile(r(?Pbw+b)s(?P=f)matchResult=pattern.search(x)x=x.replace(matchResult.group(0),matchResult.group(1)print(x)3.编写程序,用户输入一段英文,然后输出这段英文中所有长度为3个字母的单词。import rewords=input(Input the words:)l=re.s

13、plit(. +,words) #使用空格分隔词语,得到各个单词print(l)i=0 #这里我设置的是计数器for i in l: if len(i)=3: #如果单词的长度为3 输出 print(i) else: print()4.求s=a+aa+aaa+aaaa+aa.a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。num=int(input(请输入一个数字)count=int(input(请输入数字的位数)sum=0temp=0for i in range(count): temp+=num*10*i print(tem

14、p) sum+=tempprint(sum)5. 一个数如果恰好等于它的因子之和,这个数就称为完数。例如6=123。编程找出1000以内的所有完数。#encoding:utfn=input(Please input a number!)sum=0printS=for i in range(1,n): if n % i=0: sum=sum+iif sumn: print %d is 不足数%nelif sumn: print %d is 丰沛数%nelse: print %d is 完数%n printS= str(n) + = for i in range(1,n): if n% i=0:

15、printS+= str(i) + + print printS0:len(printS)-16. 打印出如下图案。 (1)for i in range(1,10): print( *(10-i),end=) print(str(i)*(2*i-1)(2)n=123456789for i in n-1:0:-1: s=n-1:int(i)-2:-1 print (%s*%d+%d=%d%(s,9,int(i)-2,int(s)*9+int(i)-2)实战作业1、输入一串字符串,判断它是不是回文数。如Madam,ImAdam是回文数。提示:要把一串字符中所有的标点符号去除,并把所有的大写字符变成

16、小写字符再进行比较。如“Madam,ImAdam“,转化后变成“madamimadam”,正读与逆读是一样的,为回文。import string #导入字符串函数库 string.punctuation=!#$%&()*+,-./:;?_|string.whitespace=tnx0bx0cr import stringoriginalStr=raw_input(enter a string:)modifiedStr=originalStr.lower()badChars=string.whitespace+string.punctuationfor char in modifiedStr:

17、if char in badChars: modifiedStr=modifiedStr.replace(char,)print the originalStr is: %snthe modifiedStr is: %snthe reverseStr is: %s%(originalStr,modifiedStr,modifiedStr:-1)if modifiedStr=modifiedStr:-1: print 字符串%s是回文%originalStrelse: print 字符串%s不是回文%originalStr2. 计算Poker中出现各种手数的概率,现给出Poker-hand-te

18、sting.data文件,其中有1000000条记录,要求读出每一条记录,并统计以下问题:以下是每条记录的格式 : 3, 10, 1, 7, 2, 12, 4, 2 , 2, 1, 04, 9, 4, 12, 4, 13, 2, 6 , 3, 4, 03, 2, 2, 2, 3, 12, 3, 1 , 4, 2, 34, 11, 2, 8, 1, 13, 4, 7 , 1, 7, 14, 8, 3, 8, 1, 3, 1, 4 , 3, 7, 12, 7, 2, 5, 3, 10, 4, 13 , 3, 7, 11, 4, 3, 4, 3, 5, 4, 10 , 2, 10, 2以下是各种类型

19、的牌的编号: 图4-9 牌的类型要求:(1)计算Poker有多少行;(2)计算包含1对牌的总手数与出现的概率;(3)计算所有牌类型出现的手数及概率 。# count poker hands# 1. open the poker data file for readingpoker_file = open(poker-hand-testing.data,r)total_count_int = 0 # 2. create and initialize variable to hold the total countpair_count_int = 0 # create and initialize

20、 variable to hold pair count# 3. Loop through each line of the filefor line_str in poker_file: total_count_int = total_count_int + 1 # (a). add one total for each hand fields = line_str.split(,) # (b). split on a comma hand_rank_str = fields-1 # and get the last field hand_rank_int = int(hand_rank_s

21、tr) if hand_rank_int = 1: #(c) if handRank is 1 (it is a pair) pair_count_int = pair_count_int + 1 # add one to pair count print(Total hands in file: , total_count_int) # 4. print the valuesprint(Count of pair hands: , pair_count_int)print(Probability of a pair: :9.4%.format(pair_count_int/total_cou

22、nt_int)3.智多星游戏:计算机随机产生4种不相同的颜色序列,玩家不知道,让玩家输入四种颜色,与计算机随机产生的序列作比较,如果全部相同则显示猜对了,否则重新输入,设定总的输入次数,超过总次数,则失败。要求如下:(1)设定总的尝试次数。(2)如果输入的颜色与随机序列在位置与颜色都相符,则打印“”。(3)如果输入的颜色与随机序列的颜色相符,但是位置上不相符,则打印“”。(4)如查输入的颜色与位置都不对,则打印“”。(5)当输入的颜色与位置都对了,就显示猜对了。(6)如果超过总的输入次数,就显示失败。#encoding:gbkimport randomimport winsoundi=0s=R

23、YBGCOPWstr=j=0corr=#产生四个随机颜色与位置,并不能重复while j4: tempChar=random.choice(s)#生成一个随机字符 if tempChar not in str: str+=tempChar j+=1#循环12次while i mydict= #创建一个空的字典 mydict #输出此字典的内容(2) 通过dict函数创建字典dict函数是字典类的构造函数,也可以利用此函数来创建字典。创建一个空字典: dict()3、(1)get函数:访问字典成员get()函数根据key获取值。 d=one:1,two:2,three:3 print(d.get

24、(two)(2)copy函数:返回一个具有相同键值的新字典 x=one:1,two:2,three:3,test:a,b,c #创建一个字典x print(x) #输出字典xone: 1, two: 2, test: a, b, c, three: 3 (3)pop函数:删除字典中对应的键Pop函数可以删除字典中的键及其对应的值。 d=one:1,two:2,three:3 d.pop(two) #删除键 two(4)fromkeys函数:用给定的键建立新的字典fromkeys函数可以用给定的键建立新的字典,键默认对应的值为None d=dict.fromkeys(one,two,three)

25、(5)update函数:用一个字典更新另外一个字典update函数可以用一个字典来更新另外一个字典。操作如下: d= one:123, two:2, three:3 实战作业:1、略(参考书中字典操作举例)2、def getdict(phone): A=i for i in range(0,10) B=zero,one,two,three,four,five,six,seven,eight,nine mydict=dict(zip(A,B) for i in phone: print mydictint(i) def main(): phone=raw_input(Please enter a

26、 series phone number:) getdict(phone)main()3、import stringdef Mostran(wholetext): f=open(e:Mos.txt,r) Mostext= for line in f: Mostext+=line f.close() Lwhole=Mostext.split() L1=Lwhole:2 #这种间隔分片出来就是列表 L2=Lwhole1:2 MosDict=dict(zip(L1,L2) for char in wholetext: print MosDictchardef main(): temp=raw_input(Enter a passage:) temp=temp.upper() wholetext= for char in temp: if char not in string.whitespace+string.punctuation: wholetext+=char translation=Mostran(wholetext)main()第9章习题答案一 选择题1.B 2.C 3.B 4.D二 简答题1、什么是异常?答:异常是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行。一般情况下,在Python无法正常处理程序时就

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服