python核心编程 第五章练习题答案(简单的没上)

#习题5-5
while True:
	usr = int(eval(raw_input('Enter your $cents: ').strip()))
	while usr in range(0,101):
		break
	else:
		print 'OverIndex~!'
		usr = int(eval(raw_input('Enter your $cents: ').strip()))
	re25 = usr%25
	if re25 == 0:
		print "needs '%i' 25cent  coins" % (usr/25,)
	elif re25%10 == 0:
		print "needs '%i' 25cent  and '%i' 10cent  coins" % ((usr-re25)/25,(re25)/10)
	elif (re25%10)%5 == 0:
		print "needs '%i' 25cent and '%i' 10cent  and '%i' 5cent  coins" %  \
		((usr-re25)/25,(re25-(re25%10))/10,(re25%10)/5)
	else:
		print "needs '%i' 25cent  and '%i' 10cent  and '%i' 5cent  and '%i' 1cent coins" %  \
		((usr-re25)/25,(re25-(re25%10))/10,(re25%10-(re25%10)%5)/5,(re25%10)%5)
#习题5-6
<pre name="code" class="python">import operator
'''
	the module enables users to input an expression,then it will gives the result
	of the expression as recognized the operator and operands
'''
usript = raw_input('enter an expression(int type only!): ').strip()
operators = {'+':operator.add,'-':operator.sub,'/':operator.truediv,'%':operator.mod,'**':operator.pow,'*':operator.mul}
newls = []
for ops in operators.keys():
	if '**' in usript:
		ops = '**'
	if ops in usript:
		newls.append(ops)
		usr = usript.split(ops)
		for i in usr:
			i.strip()
			newls.append(int(i))
		break
try:
	result = operators[ops](newls[1],newls[2])
except ZeroDivisionError,e:
	print 'Error occurs: ',e
else:
	print 'the result is:',result

#5-15
#! usr/bin/env python
arg1,arg2 =raw_input("enter two nums to find their max common divisor:  ").strip().split(",")
arg1 = int(arg1)
arg2 = int(arg2)
def maxcomdiv(num1,num2):
        '''this function can find the max common divisor of two numbers(int only)'''
        maxn = max(num1,num2)
        minn = min(num1,num2)
        try:
                if  maxn%minn == 0:
                        print 'the max common divisor:',minn
                        return minn
                else:
                        maxcomdiv(minn,maxn-minn)
        except ZeroDivisionError,e:
                print 'Error occurs:', e

def mincomul(num1,num2):
        '''this function can find the max common multiple of two numbers(int only)'''
        maxn = max(num1,num2)
        minn = min(num1,num2)
        if not maxn%minn:
                comul = maxn
        elif maxcomdiv(maxn,minn) == 1:
                comul = maxn*minn
        else:
                i = 2
                while True:
                        if not (maxn*i)%minn:
                                comul = maxn*i
                                break
                        else:
                                i+=1
        print 'the minimal common multiple is:',comul                                       
if __name__=='__main__':
        maxcomdiv(arg1,arg2)
        mincomul(arg1,arg2)
#5-16
menu = "Pymt#\tPaid\t\tAmount Remaining Balance\n-----\t------\t\t----------" 
openb = eval(raw_input('Enter opening balance: ').strip()) 
monp = eval(raw_input('Enter monthly payment: ').strip()) 
print menu 
paid = 0.00 
for i in xrange(0,1000000): 
    print i,'\t$',paid,'\t\t$',openb 
    if paid == 0.00: 
        paid = monp  
    if openb<monp: 
        paid = openb 
    if openb == 0.00: 
        break 
    openb-=paid

#5-17
import random 
lists=[] 
for i in range(0,random.randrange(0,101)): 
    n = random.randrange(0,2**31) 
    lists.append(n)  
while True: 
    try: 
        newlist =[] 
        for i in range(0,random.randrange(0,101)): 
            newlist.append(lists[i]) 
        print newlist 
        break 
    except IndexError , e: 
        print 'Error occurs:',e 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zend0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值