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
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


 
  

你可能感兴趣的:(python)