python核心编程-第5章课后练习答案

5-2. 操作符

(a)写一个函数,计算并返回两个数的乘积

(b)写一段代码调用这个函数,并显示它的结果

def product(a.b):
    return a * b

if __name__ == "__main__":
    product(2,5)

5-3. 标准类型操作符,写一段脚本,输入一个测验成绩,根据下面标准,输出他们的评分成绩(A-F)

A:90~100     B:80~89     C:70~79        D:60~69     F<60


def num(socre):  
    if 90<= socre <= 100:  
        print 'you socre is A!'  
    elif 80<= socre <=89:  
        print 'you socre is B!'  
    elif 70<= socre <=79:  
        print 'you socre is C!'  
    elif 60<= socre <=69:  
        print 'you socre is D!'  
    else:  
        print 'you socre is F!'  
      
  
while 1:  
    print '(1)Test socre(0~100).\n (2)qiut'  
    choice = raw_input('enter you choice:')  
    if choice == '1':  
        socre = raw_input('enter you socre:')  
        num(socre)  
    elif choice == '2':  
        print "qiut,Bye"  
        break  
    else:  
        print "worry,enter again!"


5-4 取余。判定给的年份是否是闰年,使用以下公式。

一个闰年就是指它可以被4整除,但是不能被100整除,或者它既可以被4又可以被100整除。

#coding=utf-8
#判断闰年


year = int(raw_input("Enter a year, to determine whether it is a leap year(1900~2016):"))
if year < 1900 or year > 2016:
	print "Out of range"
else:
	if year%4 == 0 and year%100 !=0:
		print "%d Is a leap year" %year
	elif year % 400 == 0:
		print "%d is a leap year." %year
	else:
		print "Is not a leap year"


5-5取余。取任意小于1美元的金额,然后计算可以换成最少多少枚硬币。硬币有1美分、5美分、10美分、25美分4种。1美元=100美分。举例来说,0.76美元换算结果因该是:3枚25美分+1枚1美分。类似76枚1美分,2枚25美分+2枚10美分+1枚5美分+1枚1美分 这样的结果都是不符合要求的。


coding=utf-8

	
enterCent = float(raw_input("Enter the amount of money you want to change:"))
Cent = enterCent * 100
if enterCent < 1:
	N25 = Cent/25
	Cent10 = (Cent - N25*25)/10
	Cent5 = (Cent - N25*25 - Cent10*10)/5
	Cent1 = Cent - N25*25 - Cent10*10 - Cent5*5
	print "%d can be split into: 25美分:%d 10美分:%d 5美分:%d 1美分:%d." \
	%(enterCent,N25,Cent10,Cent5,Cent1)
else:
	print "worry enter."

自己的答案总是不能显示1美分,不知道问题出在哪里,下面写出别人的答案

#!/usr/bin/python

try:
        dollar = float(raw_input("Input the money that less than 1 dollar:"))
        if dollar >= 1:
                print "money is too large."
        elif 0 < dollar < 1:
                temp = int(dollar * 100)
                (N25, temp) = divmod(temp, 25)
                print "%d 25 coins." %N25
                (N10, temp) = divmod(temp, 10)
                print "%d 10 coins." %N10
                (N5, temp) = divmod(temp, 5)
                print "%d 5 coins." %N5
                (N1, temp) = divmod(temp, 1)
                print "%d 1 coins." %N1
        else:
                print "You must input larger than 0."
except ValueError, e:
        print "You must input a digits."


5-6. 算数。写一个计算器程序。你的代码可以接受这样的表达式,两个操作数加一个操作符:N1操作符N2.其中N1和N2为整型或浮点型,操作符可以使+、-、*、/、%、**计算这个表达式结果,然后显示出来。提示:可用字符串split(),但不可使用内建函数eval()

while 1:  
    enter=raw_input('number1 (+|-|*|/|%|**) number2: ')  
    if '+' in enter:  
        a,b=enter.split('+',1)  
        return float(a)+float(b)  
    elif '-' in enter:  
        a,b=enter.split('-',1)  
        return float(a)-float(b)  
    elif '**' in enter:  
        a,b=enter.split('**',1)  
        return float(a)**float(b)  
    elif '/' in enter:  
        a,b=enter.split('/',1)  
        return float(a)/float(b)  
    elif '%' in enter:  
        a,b=enter.split('%',1)  
        return float(a)%float(b)  
    elif '*' in enter:  
        a,b=enter.split('*',1)  
        return float(a)*float(b)  
    else:  
        print "wrong enter,qiut"  
        break

5-7. 营业税。随意取一个商品金额,然后根据当地营业税额度计算应该交纳的营业税。

#coding=utf-8

def busnissTax(turnover,tax=0.2):
	return turnover * tax

choice = raw_input("1.使用默认税率.\n 2.输入税率.")
if choice == '1':
	turnover = float(raw_input('Input amount:'))
	print "你商品的营业税为:%d." % busnissTax(turnover)
elif choice == '2':
	turnover = raw_input("enter turnover:")
	tax = float(raw_input("the default rate is:"))
	print "你商品的营业税为:%d." % float(turnover * tax)
else:
	print "worry enter."


5-8 到5-15没时间写,先略过稍后由时间补上

5-16 家庭财务,给定一个初始基金金额和月度开销数,使用循环,剩下的金额和当月的支出数,包括最后支出。


自己改了一下,循环输入不同支出,但是不能一起显示,打算日后用txt写入然后输出显示,稍后有时间补上

#coding=utf-8

def Payment(amount,expenditure):
	print "pym#     Paid      Blance"
	print "%d       $ %d      $ %d" %(count,expenditure,float(amount))
	
amount = float(raw_input("Enter opening balance:"))
count = 0
while 1:
	expenditure = float(raw_input("enter monter payment:"))
	amount -= expenditure
	Payment(amount,expenditure)
	count += 1
	if amount <= 0:
		print "No money!"
		break


你可能感兴趣的:(python核心编程)