python课后题答案第五章_python 核心编程 课后练习题(第五章)

很久没用Python了,只能自己找机会练一练,还是有不少收获的

Ex53:1 #!/usr/bin/env python

2

3

4

5

6 def evaluate(score):

7 if score>=90:

8 return 'A'

9 elif score>=80:

10 return 'B'

11 elif score>=70:

12 return 'C'

13 elif score>='60':

14 return 'D'

15 else:

16 return 'F'

17

18 print evaluate(10)

19 print evaluate('af')

Ex54:1 #!/usr/bin/env python

2

3

4 isleapyear=lambda year:bool(not year%4 and year%100) or bool(not year%400)

5

6

7 print isleapyear(1992)

8 print isleapyear(1996)

9 print isleapyear(2000)

10 print isleapyear(1900)

11 print isleapyear(1997)

Ex55:1 #!/usr/bin/env python

2

3

4

5 def returncharge(money,cointype):

6 cointype.sort(reverse=True)

7 for cnt in range(len(cointype)):

8 coin_num,money=divmod(money,cointype[cnt])

9 print cointype[cnt],":",coin_num

10

11

12 returncharge(90,[1,10,5,25])

Ex56: (无聊用了下工厂模式)1 #!/usr/bin/env python

2

3

4 class Operation(object):

5 def __init__(self,n1,n2=None):

6 self.n1=n1

7 self.n2=n2

8 def getresult(self):

9 pass

10

11 class OperationAdd(Operation):

12 def getresult(self):

13 return self.n1+self.n2

14

15 class OperationSub(Operation):

16 def getresult(self):

17 return self.n1-self.n2

18

19 class OperationMul(Operation):

20 def getresult(self):

21 return self.n1*self.n2

22

23 class OperationDiv(Operation):

24 def getresult(self):

25 return self.n1/self.n2

26

27 class OperationPow(Operation):

28 def getresult(self):

29 return self.n1**self.n2

30

31 class OperationFactory(object):

32 @staticmethod

33 def createoperation(expression):

34 if expression.find('+'):

35 n1,n2=expression.split('+')

36 return OperationAdd(n1,n2)

37 if expression.find('-'):

38 n1,n2=expression.split('-')

39 return OperationSub(n1,n2)

40 if expression.find('*'):

41 n1,n2=expression.split('*')

42 return OperationMul(n1,n2)

43 if expression.find('/'):

44 n1,n2=expression.split('/')

45 return OperationDiv(n1,n2)

46 if expression.find('**'):

47 n1,n2=expression.split('**')

48 return OperationPow(n1,n2)

49

50

51 x=OperationFactory.createoperation('1*1')

52 print x.n1

53 print x.n2

54 print x.getresult()

Ex58:(又偷懒了)1 #!/usr/bin/env python

2

3 PI=3.14

4

5

6 class Shape2D(object):

7 def getarea(self):

8 pass

9

10 class Shape3D(object):

11 def getvolume(self):

12 pass

13

14 class Retangle(Shape2D):

15 def __init__(self,e1,e2):

16 self.e1=e1

17 self.e2=e2

18 def getarea(self):

19 return e1*e2

20

21 class Round(Shape2D):

22 def __init__(self,r):

23 self.r=r

24 def getarea(self):

25 return PI*self.r*self.r

26

27 x=Round(5)

28 print x.getarea()

Ex510:1 #!/usr/bin/env python

2

3 from __future__ import division

4

5 C=lambda F:(F-32)*(5/9)

6

7 print C(100)

Ex511:1 #!/usr/bin/env python

2

3

4 def geteven(min_num,max_num):

5 return [x for x in range(min_num,max_num+1) if not x%2]

6

7

8 def getodd(min_num,max_num):

9 return [x for x in range(min_num,max_num+1) if x%2]

10

11 def ifdivisible(n1,n2):

12 return not n2%n1

13

14 print geteven(0,20)

15 print getodd(0,20)

16 print ifdivisible(5,25)

偷懒了几道题

Ex517:

1 #!/usr/bin/env python

2

3 import random

4

5 N=random.randint(2,100)

6 num_list=[]

7

8 for i in range(N):

9 num=random.uniform(0,2**31-1)

10 num_list.append(num)

11

12 N2=random.randint(2,100)

13 num_list2=[]

14 for i in range(N2):

15 num2=random.choice(num_list)

16 num_list2.append(num2)

17

18 print num_list2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值