设为首页 收藏本站
查看: 1138|回复: 0

[经验分享] Python核心编程(第二版)课后习题部分代码(3-4章)(持续更新)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-5-7 15:06:46 | 显示全部楼层 |阅读模式
本人近期研究了下Python,所用的书籍主要是Python核心编程(第二版)等,在此期间,我对书后的习题(编码部分)进行了实现,由于此书课后没有答案(英文版的答案也不全,我看的是电子书,不知道买的书后有没有答案),所以,在此把我的实现分享一下,希望对初学者有帮助。由于本人水平有限,在实现的编码中肯定有不合理的地方,希望大虾指出,我及时更正,大家共同交流。
     3-12(第三章当时做的时候有些没实现,有时间补上)
  
'''
@author: Riquelqi
'''
#!/user/bin/env python
import os
ls=os.linesep
def write():
content=[]
#Input a filename.
fname=raw_input("Please input a filename:%s"%ls)
print "Please input content(Quit with 'q'):"
lines=raw_input(">")
while lines!="q":
content.append(lines)
lines=raw_input(">")
afile=open(fname,"w")
afile.writelines("%s%s"%(line,ls)for line in content)
afile.close()
print "DONE!",
def read():
#input a filename
fname=raw_input('Please input a filename:%s'%ls)
#To be sure that the file path  exists
if not os.path.exists(fname):
print"File doesn't exists!"
else:
afile=open(fname,'r')
for line in afile.readline():
print line,
print '\nDONE!'
if __name__=='__main__':
input=raw_input("""
|Please input your commend|
--|-------------------------|--   
--|  r        |   read      |--
--|  w        |   write     |--
--|  q        |   quit      |--
|Please input your commend|      
""" )
while input!='q':
if input=='r':
read()
elif input=='w':
write()
input=raw_input("""
|Please input your commend|
--|-------------------------|--   
--|  r        |   read      |--
--|  w        |   write     |--
--|  q        |   quit      |--
|Please input your commend|      
""" )      

    4-6(第四章感觉没有什么,不过以下我的这段练习,有助于4-6题的理解,并且这段代码也说明了很多知识)
  
'''
@author: Riquelqi
'''
# To import like this is  better than import types ,because we will use the property like that types.IntType...
# It will select twice
from types import IntType,FloatType,LongType,ComplexType
def display_Number_Best(number):
print number, 'is',
if isinstance(number,(int,long,float,complex)):
print 'a number of type:',type(number).__name__
else:
print 'not a number at all',
def display_Number_Worst(number):
print number,'is'
if type(number)==type(0):
print 'a number of type:',type(0).__name__
elif type(number)==type(0L):
print 'a number of type:',type(0L).__name__
elif type(number)==type(0.0):
print 'a number of type:',type(0.0).__name__
elif type(number)==type(0.0+0.0j):
print 'a number of type:',type(0.0+0.0j).__name__
else :
print 'not a number at all'
def display_Number_Better(number):
print number,'is'
if type(number) is IntType:
print 'a number of type: int'
elif type(number) is FloatType:
print 'a number of type:float'
elif type(number) is LongType:
print 'a number of type:Long'
elif type(number) is ComplexType:
print 'a number of type:Complex'
else:
print 'not a number at all'
def display_Number_Good(number):
print number,'is'
if type(number) == IntType:
print 'a number of type: int'
elif type(number) == FloatType:
print 'a number of type:float'
elif type(number) == LongType:
print 'a number of type:Long'
elif type(number) == ComplexType:
print 'a number of type:Complex'
else:
print 'not a number at all'      
if __name__=='__main__':
display_Number_Best(94)
display_Number_Best(-64)
display_Number_Best(10.3232)
display_Number_Best(777777777777777777777777)
display_Number_Best(10.0+10.0j)
display_Number_Best('qq')
#    display_Number_Worst(554454)
#    display_Number_Worst(10.11)
#    display_Number_Worst(10.0+10.0j)
#    display_Number_Worst("sdfsdfsd")   
#    display_Number_Better(45654)
#    display_Number_Better(10.0)
#    display_Number_Better(10.0+10.0j)
#    display_Number_Better(8888888888888888)
#    display_Number_Better('xxx')
#    display_Number_Good(21554)
#    display_Number_Good(8888888888888888888888888)
#    display_Number_Good(10.0)
#    display_Number_Good(10.0+10.0j)
#    display_Number_Good('my')

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-374321-1-1.html 上篇帖子: py2exe 把python脚本转成windows下可执行文件 下篇帖子: PyCon 2011
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表