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

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

[复制链接]

尚未签到

发表于 2017-5-7 08:17:37 | 显示全部楼层 |阅读模式
本人近期研究了下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

运维网声明 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-374003-1-1.html 上篇帖子: Luigi --基于Python语言的流式任务调度框架教程 下篇帖子: [总结 提供源码]基于Python MakoTemplate开发的www.tianqiyubao-5-7-10.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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