why 发表于 2017-5-7 15:06:46

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

本人近期研究了下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 pathexists
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 isbetter 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]
查看完整版本: Python核心编程(第二版)课后习题部分代码(3-4章)(持续更新)