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

本人近期研究了下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、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值