Technology Learning

导航

python 核心编程课后练习(chapter 5)

5-2

1 #5-2
2 def mul(x, y):
3   return x * y
4 
5 print mul(4,5)  
View Code

5-3

 1 #5-3
 2 def value_score(num):
 3   if 90<=num<=100:
 4     return 'A'
 5   elif 80<=num<=89:
 6     return 'B'
 7   elif 70<=num<=79:
 8     return 'C'
 9   elif 60<=num<=69:
10     return 'D'
11   elif 0<=num<=59:
12     return 'F'
13   else:
14     print "invalid value"         
15 
16 print value_score(90)
17 print value_score(1000)
18 print value_score(0) 
View Code

5-4

 1 #5-4
 2 def Leap_year(year):
 3   if year%4 == 0 and year%100 == 0 :
 4     return True
 5   elif year%4 == 0 and year%100 != 0 :
 6     return True   
 7   else:
 8     return False
 9 
10 if Leap_year(2004):
11   print "leap year"
12 else:
13   print "common year" 
View Code

5-5

 1 #5-5
 2 import random
 3 
 4 def least_cents(cts):
 5   left = 0
 6   sum = 0
 7 #get the coins of 25cents  
 8   sum = cts/25
 9   left = cts%25
10 #get the coins of 10cents  
11   sum += left/10
12   left = left%10
13 #get the coins of 5cents  
14   sum += left/5
15   left = left%5
16 #get the coins of 1cents  
17   sum += left
18   
19   return sum
20 
21 cents = random.randint(1, 100)
22 
23 print "%d cents will have at least: %d coins" % (cents, least_cents(cents))
View Code

5-6

 1 #5-6
 2 
 3 def add(x, y):
 4   return x + y
 5 
 6 def sub(x, y):
 7   return x - y
 8 
 9 def mul(x, y):
10   return x * y
11 
12 def div(x, y):
13   return float(x)/float(y)      
14 
15 def mod(x, y):
16   return x % y
17 
18 def pow(x, y):
19   return x ** y
20 
21 def cal(str):
22   a = []
23   if(len(str.split('**')) == 2):
24     a = str.split('**')    
25     return pow(int(a[0]), int(a[1]))
26   elif (len(str.split('*')) == 2):
27     a = str.split('*')
28     return mul(int(a[0]), int(a[1]))
29   elif (len(str.split('%')) == 2):
30     a = str.split('%')
31     return mod(int(a[0]), int(a[1]))
32   elif (len(str.split('+')) == 2):
33     a = str.split('+')
34     return add(int(a[0]), int(a[1]))  
35   elif (len(str.split('-')) == 2):
36     a = str.split('-')
37     return sub(int(a[0]), int(a[1])) 
38   else:
39     print "Not support"  
40 
41     
42 print "enter the formula:"
43 formula_str = raw_input()          
44 print cal(formula_str)    
View Code

5-7

1 #5-7 
2 def tax(num):
3   return num * 0.03
4 
5 turnover = raw_input("enter the turnover:")
6 print"the tax you need to turn over to the state: %f" %(tax(int(turnover)))
View Code

5-8

 1 #5-8 
 2 import math
 3 def suqare_area(len):
 4   return float(len) * float(len)
 5 
 6 def suqare_volume(len):
 7   return float(len) ** 3
 8 
 9 def circle_area(r):
10   return math.pi * float(r) * float(r)
11 
12 def spere_volum(r):
13   return 4 * math.pi * float(r) * float(r) * float(r)/ 3.0
14   
15 
16 len = raw_input("enter the len:")
17 
18 print "the area of suqare is: %f" % suqare_area(len)
19 print "the volume of square is: %f" % suqare_volume(len)       
20 
21 r = raw_input("enter the r:")
22 print "the area of the circle is: %f" % circle_area(r) 
23 print "the volume of spere is: %f" % spere_volum(r)
View Code

5-10

1 #5-10
2 def fah_to_centi(c):
3   return (c - 32)*(5/float(9))
4   
5 c = raw_input("enter the fahrenheit:")
6 
7 print "the centi degrees is: %f" % fah_to_centi(float(c))
View Code

5-11

 1 #5-11
 2 def odd_num(num):
 3   return num %2 == 1
 4 
 5 def aliquant(n1, n2):
 6   return n1 % n2 == 0
 7 
 8 for e in range(0, 20):
 9   if not odd_num(e):
10     print "even number:%d" % e
11 
12 for e in range(0, 20):
13   if odd_num(e):
14     print "odd number: %d" % e  
15     
16 n1 = raw_input("enter the first number:")
17 n2 = raw_input("enter the second number:")
18 if aliquant(int(n1), int(n2)):
19   print"aliquant"
20 else:
21   print"not aliquant"  
View Code

5-12

1 #5-12   
2 import sys
3 
4 print sys.maxint
5 print -sys.maxint - 1
6 
7 print sys.float_info.max
8 print sys.float_info.min
View Code

5-13

1 #5-13
2 
3 def hm_to_m(str):
4   a = str.split(':')
5   return int(a[0])* 60 + int(a[1])
6   
7 hm = raw_input("enter the time:")
8 
9 print "the currents mins is: %d" % hm_to_m(hm)  
View Code

5-14

1 #5-14
2 def year_rate():
3   return (1+0.0385)** 365 - 1.0
4 
5 print year_rate()
View Code

5-15

 1 #5-15
 2 def GCD(m,n):
 3   if m%n == 0:
 4     return n
 5   else:
 6     return GCD(n, m%n) 
 7 
 8 def LCM(m, n):
 9   return m*n/GCD(m, n)
10 
11 m = int(raw_input("enter m:"))
12 n = int(raw_input("enter n:"))
13 
14 print"the GCD of %d, %d is: %d" % (m, n, GCD(m, n))
15 print"the LCM of %d, %d is: %d" % (m, n, LCM(m, n))  
View Code

 

5-17

 1 #5-17
 2 import random
 3 
 4 N = random.randint(2, 100)
 5 a = []
 6 
 7 def show_all(a):
 8   for i in range(len(a)):
 9     print a[i]
10 
11 for i in range(0, N-1):
12   tmp = random.randint(2, 2**31)
13   a.append(tmp)
14 print "Original:"
15 show_all(a)
16 
17 for i in range(0, N-1):
18   for j in range(i, N-1):
19     if a[i] > a[j]:
20       a[i], a[j] = a[j], a[i]
21 
22 print"New arrary:"
23 show_all(a)  
View Code

 

 

 

 

 

 

 

 

 

posted on 2013-08-05 19:48  浔阳渔夫  阅读(295)  评论(0编辑  收藏  举报