python核心编程 第二版 第五章 习题

来源:互联网 发布:centos如何安装ssh 编辑:程序博客网 时间:2024/06/09 16:44
初学python,将自己练习的代码发在这存着,若有人看到,希望不吝赐教。~

-----------------------------------------------------------------------------------------------------------------------

5-2:

def product(a, b):print 'The product of a & b is %s ', (a * b)a = float(raw_input('a is '))b = float(raw_input('b is '))print product(a, b)

5-3:

def grade(score):print '''A: 90~100B: 80~89C: 70~79D: 60~69F: <60'''if 90 <= score <= 100:return "The Grade is A"elif 80 <= score <= 89:return "The Grade is B"elif 70 <= score <= 79:return "The Grade is C"elif 60 <= score <= 69:return "The Grade is D"elif 0 <= score < 60:return "The Grade is F"else:return "Pleas print the number in scope!"score = float(raw_input('The score is '))print grade(score)

5-4:

print 'Which year you want to check?'year = int(raw_input('Input the year >>'))def isleapyr(year):if year % 4 != 0:return "This year isn't leap year!"elif year % 4 == 0:if year % 400 == 0:return "This year is leap year."elif year % 400 != 0 and year % 100 == 0:return "Sorry, but this isn't leap year."else:return "This is leap year."print isleapyr(year)

5-5:

print "You want to exchange some coin?"print "I have four kind of coins."print '1 cent, 5 cents, 10 cents and 25 cents.'print "How much do you want?"def change(input):while True:amount = float(raw_input('less then 1$ >>'))if amount >= 1:print "I don't have enough coins to exchange!"else:breakif 0.00 < amount < 1.00:amount = 100 * amountc25 = amount / 25rest25 = amount % 25c10 = rest25 / 10rest10 = rest25 % 10c5 = rest10 / 5rest5 = rest10 % 5c1 = rest5 / 1return "Here are %d 25cents, %d 10cents, %d 5cents and %d 1cents."  % (c25, c10, c5, c1)print change(input)

5-6:

print "I make a calculator."print "You can input 2 numbers, 1 operator."print "And I'll give you the answer."def calculator(input):while True:input = str(raw_input("Tell me your expression >> "))expr = input.split()if len(expr) != 3:print "Please use the 'blank' to slice the expression."else:breaka = int(expr[0])b = int(expr[2])c = expr[1]if c == '+':return a + belif c == '-':return a - belif c == '*':return a * belif c == '/':return a / belif c == '**':return a ** bprint calculator(input)

5-8a:

def square(a):return (a ** 2)def cube(a):return a ** 3while True:print '''(s)quare(c)ube(q)uit  '''choice = str(raw_input('Make your choice >> '))if choice in 'scq':if choice == 's':a = int(raw_input('Tell me the length of side >>'))print "The answer is ", square(a)elif choice == 'c':a = int(raw_input('Tell me the length of side >>'))print "The answer is ", cube(a)elif choice == 'q':print "Thanks for using. Bye!"breakelse:print 'I don\'t know, try again!'

5-8b:

def sphere(r):return 3.0 / 4.0 * 3.14 * (r ** 3)def circle(r):return 3.14 * (r ** 2)while True:print '''(s)phere(c)ircle(q)uit  '''choice = str(raw_input('Make your choice >> '))if choice in 'scq':if choice == 's':r = int(raw_input('Tell me the radius >>'))print "The answer is ", sphere(r)elif choice == 'c':r = int(raw_input('Tell me the radius >>'))print "The answer is ", circle(r)elif choice == 'q':print "Thanks for using. Bye!"breakelse:print 'I don\'t know, try again!'

5-9:

0 0
原创粉丝点击