本人近期研究了下Python,所用的书籍主要是Python核心编程(第二版)等,在此期间,我对书后的习题(编码部分)进行了实现,由于此书课后没有答案(英文版的答案也不全,我看的是电子书,不知道买的书后有没有答案),所以,在此把我的实现分享一下,希望对初学者有帮助。由于本人水平有限,在实现的编码中肯定有不合理的地方,希望大虾指出,我及时更正,大家共同交流。
5_2_return_Product
'''
@author: Riquelqi
'''
def return_Product(x,y):
print "'",x,"' multiply '",y ,"' equal" ,
return x*y
if __name__ == '__main__':
print return_Product(1, 2)
5_3_attainment_Test
'''
@author: Riquelqi
'''
def attainment_Test(x):
if x<0 or x>100 :
print 'It is a invalid number!'
elif x<60:
print "F"
elif 60<= x <=69:
print"D"
elif 70<= x <=79:
print 'C'
elif 80<= x <=89:
print 'B'
else :
print 'A'
if __name__ == '__main__':
attainment_Test(60)
5_4_leap_year_Test
'''
@author: Riquelqi
'''
def leap_year_Test(x):
if not isinstance(x,(int,long)):
print 'It is not a year!'
else :
if not x%4:
if not x%100:
print 'The year ',x,' is a leap year'
else :
print 'The year',x,'is not a leap year!'
if __name__ == '__main__':
leap_year_Test(2400)
5_5_coin_Convert
'''
@author: Riquelqi
'''
#The parameter x represent dollar
def coin_Convert(x):
aDollar=x*100
if aDollar<5:
print below_5_cent(aDollar)
elif aDollar<10:
print above_5_below_10_cent(aDollar)
elif aDollar<25:
print above_10_below_25_cent(aDollar)
else:
print above_twenty_five_cent(aDollar)
def below_5_cent(x):
return x,'coin of one cent!'
def above_5_below_10_cent(x):
return '1 coin of five cent and ',x%5,'coin of one coin'
def above_10_below_25_cent(x):
result_Tuple=divmod(x, 10)
temp=0
if result_Tuple[1]<5:
temp=below_5_cent(result_Tuple[1]),
else:
temp=above_5_below_10_cent(result_Tuple[1]),
return result_Tuple[0],'coin of ten cent and ',temp ,
def above_twenty_five_cent(x):
result_Tuple=divmod(x, 25)
temp=0
if result_Tuple[1]>10:
temp=above_10_below_25_cent(result_Tuple[1])
elif result_Tuple[1]>5:
temp=above_5_below_10_cent(result_Tuple[1])
else:
temp=below_5_cent(result_Tuple[1])
return result_Tuple[0],'coin of twenty-five cent and ',temp
if __name__ == '__main__':
coin_Convert(0.76)
5_6_calculator_implement
'''
@author: Riquelqi
'''
# >>> a='1.0+2.0'
# >>> a
# >>> '1.0+2.0'
# >>> b=a.count('.',0,len(a))
# >>> b
# >>> 2
#
def calculator_implement(x):
if x.count('**',0,len(x)):
q_list=x.split('**')
if x.count('.',0,len(x)):
return float(q_list[0])**float(q_list[1])
else:
return int(q_list[0])**int(q_list[1])
else:
#if expression contains a float
if x.count('.',0,len(x)):
if x.count('+',0,len(x)):
q_list=x.split('+')
return float(q_list[0])+float(q_list[1])
elif x.count('*',0,len(x)):
q_list=x.split('*')
return float(q_list[0])*float(q_list[1])
elif x.count('-',0,len(x)):
q_list=x.split('-')
return float(q_list[0])-float(q_list[1])
elif x.count('/',0,len(x)):
q_list=x.split('/')
return float(q_list[0])/float(q_list[1])
elif x.count('%',0,len(x)):
q_list=x.split('%')
return float(q_list[0])%float(q_list[1])
else:
return'operator ERROR!'
else:
q_operator=x[1]
q_list=x.split(q_operator)
if '+' ==q_operator:
return int(q_list[0])+int(q_list[1])
elif '*'==q_operator:
return int(q_list[0])*int(q_list[1])
elif '-'==q_operator:
return int(q_list[0])-int(q_list[1])
elif '/'==q_operator:
return int(q_list[0])/int(q_list[1])
elif '%'==q_operator:
return int(q_list[0])%int(q_list[1])
else:
return'operator ERROR!'
if __name__ == '__main__':
input_str=raw_input('Please input a expression:')
result=calculator_implement(input_str)
print result
5_7_calculate_Tax
'''
@author: Riquelqi
'''
from decimal import Decimal
def calculate_Tax(x):
return Decimal(x)*Decimal('0.17')
if __name__ == '__main__':
input_operand=raw_input('Please input a operand:')
result=calculate_Tax(input_operand)
print result
分享到:
相关推荐
本文为python核心编程第二版的第 2 ,3 ,5 章课后习题代码,欢迎大家下载
python核心编程第二版第7章习题答案.pdf
Python核心编程(第二版)第十章习题答案.pdf
这是python第二版中文版第二到二十三章的习题答案,希望能帮到有需要的朋友
python编程
这是Python核心编程第二版第十三章类的习题答案,从第一题到第十一题和第十九二十题,包括代码和data,如果有误还请批评指正
Python核心编程(第二版)部分习题答案 全部都是自己写的,有两或三个文件没有写完成,后来就不想写了 其中x.x是抄课文中的代码 x_x 是课后习题代码 课文中的代码有很多错误,在我的文档中都已经改过来了,并调试...
这是我做的Python核心编程第二版第十五章习题的答案,从第一题到29题的所有代码。如果有不足或错误之处,还请前辈们指正
·学习专业的Python风格、最佳实践和好的编程习惯; ·加强对Python对象、内存模型和Python面向对象特性的深入理解; ·构建更有效的Web、CGI、互联网、网络和其他客户端/服务器架构应用程序及软件; ·学习如何...