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
- Python核心编程(第二版) 第五章 课后习题(1)
- python核心编程 第二版 第五章 习题
- 《python 核心编程第二版》第五章
- 《Python核心编程》第二版课后习题——第五章 (记录自己做的习题,可能有误)
- Python核心编程 第二版 第二章 部分习题解答
- python核心编程 第二版 第二章 习题
- python核心编程第二版第二章习题
- <python 核心编程第二版课后习题>第二章
- python核心编程第五章课后习题
- python核心编程第二章习题
- Python核心编程第二章课后习题
- 《Python核心编程》第二章习题解答
- Python核心编程第二章习题
- 《python核心编程第二版》第三章习题
- python核心编程(第二版)习题_第三章
- <Python核心编程 第二版> 第四章习题
- 《python 核心编程第二版第六章》习题
- 《python 核心编程第二版第六章》习题补充
- ExtJs中getCmp、getDom、Get的区别
- Libgdx之Texture TextureRegion
- 2016第七届蓝桥杯C/C++ B组省赛第五题:抽签
- hdu4786-图论训练3-最小生成树
- VC++6.0常出现的后缀名及其含义大全
- python核心编程 第二版 第五章 习题
- Android自定义ImageView实现圆形控件显示
- 贪心算法problem L(1012)
- 组合数学训练 (16.03.29)
- C语言单链表的创建,插入,删除,逆致
- 深入理解 Java 虚拟机-Java 语法糖
- 面试中遇到的SQL题目
- 50个经典常用的sql语句
- Longest Substring with At Most K Distinct Characters