python核心编程 第五章练习题答案(简单的没上)
2014-05-20 19:22
288 查看
#习题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
相关文章推荐
- 《Python核心编程》第二版第97页第五章练习 -Python核心编程答案-自己做的-
- <Python核心编程(第二版)>第五章练习题
- Python核心编程(第二版)第五章练习题
- C语言程序设计 练习题参考答案 第五章 (1) 函数定义调用
- python核心编程(第二版)参考答案(自制)--第五章·数字
- Python核心编程(第二版)2.21练习题答案
- <Python核心编程(第二版)>第五章练习题二
- 《Python核心编程》第二版第六章练习题答案-第五部分
- 《Python核心编程》第二版第六章练习题答案-第四部分
- 《Python核心编程》第二版第五章答案
- 《Python核心编程》第二版第八章练习题答案 第一部分
- 《Python核心编程》第二版第97页第五章练习 续一 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第六章练习题答案-第三部分
- 《Python核心编程》第二版第七章练习题答案 第一部分
- 《Python核心编程》第二版第八章练习题答案 第三部分
- Python核心编程第三版——第一章练习题答案
- 《Python核心编程》第二版第六章练习题答案-第二部分
- 《Python核心编程》第二版第七章练习题答案 第二部分
- C语言程序设计 练习题参考答案 第五章 (2) 递归函数
- 《Python核心编程》第二版第六章练习题答案