---
title: "Python核心编程（第二版）课后习题部分代码（5章）（持续更新）"
author: "里克尔奇"
date: "2010-04-02T20:04:59Z"
tags: []
categories: ["编程语言"]
slug: "python-5"
draft: false
lastmod: "2010-04-02T21:15:00Z"
id: 631136
url: "https://requelqi.iteye.com/blog/631136"
---

本人近期研究了下Python，所用的书籍主要是Python核心编程（第二版）等，在此期间，我对书后的习题（编码部分）进行了实现，由于此书课后没有答案（英文版的答案也不全，我看的是电子书，不知道买的书后有没有答案），所以，在此把我的实现分享一下，希望对初学者有帮助。由于本人水平有限，在实现的编码中肯定有不合理的地方，希望大虾指出，我及时更正，大家共同交流。
5_2_return_Product

```python

'''
@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

```python

'''
@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

```python

'''
@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

```python

'''
@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

```python

'''
@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

```python

'''
@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

```