怎么找不到<Python核心编程>的习题答案

Python022

怎么找不到<Python核心编程>的习题答案,第1张

def ji(x1, x2):

'''5-2 返回两个数的乘积''' return x1*x2 def grade(score):

'''5-3 输入乘积0~100份之内,返回评分''' if 90<=score<=100: return 'A' elif 80<=score<=89: return 'B' elif 70<=score<=79: return 'C' elif 60<=score<=69: return 'D' elif 60>score: return 'F' def isleapyear(year):

'''5-4 输入年份,判断是否是闰年,年份小于172800年有效''' if (year%4==0 and year%100 !=0) or year%400==0: return True return False def minmoney(money):

'''5-5 将任意一个美元(小于1美元)分成硬币 由1美分,5美分,10美分,25美分 且硬币数量是最少的一种组合方式 '''

m1 = int(money*100) m25 = m1/25 m1 -= m25*25 m10 = m1/10 m1 -= m10*10 m5 = m1/5 m1 -= m5*5

# 1美分,5美分,10美分,25美分 return [m1,m5,m10,m25]

def computer(cmd):

'''5-6 输入类似 x * y 这样的式子,自动获得值''' ops = ['+','-','**','/','%','*'] for op in ops: if op in cmd:

cmds = cmd.split(op) cmds[0]=float(cmds[0]) cmds[1]=float(cmds[1]) if op == '+':return sum(cmds) if op == '-':return cmds[0]-cmds[1] if op == '**':return pow(cmds[0],cmds[1]) if op == '/':return cmds[0]/cmds[1] if op == '%':return cmds[0]%cmds[1] if op == '*':return cmds[0]*cmds[1] def tax(value, tax=0.17):

'''5-7 输入价格,获得营业税,这里假设税金是20%''' import decimal

value = decimal.Decimal(str(value)) tax = decimal.Decimal(str(tax)) return value*tax

def square(x,y=None):

'''5-8(a)-1 求正方形或者长方形面积''' if y == None:y = x return x*y

def cube(x,y=None,h=None): '''5-8(a)-2 求立方体的体积''' if y==None:y=x if h==None:h=x return x*y*h def circle(r):

'''5-8(b)-1 求圆的面积''' import math return 2*math.pi*r

(window.cproArray = window.cproArray || []).push({ id: "u2280119" })

def sphere(r):

'''5-8(b)-2 求球的体积''' import math

return 4./3*math.pi*r**3 def f2c(f):

'''5-10 华氏度转摄氏度 FahrenheitDegree to CelsiusDegree''' return (f-32)*(5./9) def even(l):

'''5-11(a) 求列表中数字的偶数''' rl = [] for i in l: if i%2==0: if i in rl:continue rl.append(i) return sorted(rl) def odd(l):

'''5-11(b) 求列表中数字的奇数''' rl = [] for i in l: if i%2 != 0: if i in rl:continue rl.append(i) return sorted(rl) def individe(x,y): '''5-11(d) 是否能整除''' if x%y==0: return True return False def numinfo():

'''5-12 输出当前系统关于数字的范围''' import sys l = {}

maxint = sys.maxint minint = -maxint

###len的值在不停变化,当i>len的时候就跳出了,这时候还没有遍历整个列表,删掉一个元素,对应的长度应该也减1

###12行开始

if num_num % fac_list[i] ==0:

    del fac_list[i]

    i-=1

i+=1