python 核心编程课后练习(chapter 5)
5-2
#5-2
def mul(x, y):
return x * y print mul(4,5)
5-3
#5-3
def value_score(num):
if 90<=num<=100:
return 'A'
elif 80<=num<=89:
return 'B'
elif 70<=num<=79:
return 'C'
elif 60<=num<=69:
return 'D'
elif 0<=num<=59:
return 'F'
else:
print "invalid value" print value_score(90)
print value_score(1000)
print value_score(0)
5-4
#5-4
def Leap_year(year):
if year%4 == 0 and year%100 == 0 :
return True
elif year%4 == 0 and year%100 != 0 :
return True
else:
return False if Leap_year(2004):
print "leap year"
else:
print "common year"
5-5
#5-5
import random def least_cents(cts):
left = 0
sum = 0
#get the coins of 25cents
sum = cts/25
left = cts%25
#get the coins of 10cents
sum += left/10
left = left%10
#get the coins of 5cents
sum += left/5
left = left%5
#get the coins of 1cents
sum += left return sum cents = random.randint(1, 100) print "%d cents will have at least: %d coins" % (cents, least_cents(cents))
5-6
#5-6 def add(x, y):
return x + y def sub(x, y):
return x - y def mul(x, y):
return x * y def div(x, y):
return float(x)/float(y) def mod(x, y):
return x % y def pow(x, y):
return x ** y def cal(str):
a = []
if(len(str.split('**')) == 2):
a = str.split('**')
return pow(int(a[0]), int(a[1]))
elif (len(str.split('*')) == 2):
a = str.split('*')
return mul(int(a[0]), int(a[1]))
elif (len(str.split('%')) == 2):
a = str.split('%')
return mod(int(a[0]), int(a[1]))
elif (len(str.split('+')) == 2):
a = str.split('+')
return add(int(a[0]), int(a[1]))
elif (len(str.split('-')) == 2):
a = str.split('-')
return sub(int(a[0]), int(a[1]))
else:
print "Not support" print "enter the formula:"
formula_str = raw_input()
print cal(formula_str)
5-7
#5-7
def tax(num):
return num * 0.03 turnover = raw_input("enter the turnover:")
print"the tax you need to turn over to the state: %f" %(tax(int(turnover)))
5-8
#5-8
import math
def suqare_area(len):
return float(len) * float(len) def suqare_volume(len):
return float(len) ** 3 def circle_area(r):
return math.pi * float(r) * float(r) def spere_volum(r):
return 4 * math.pi * float(r) * float(r) * float(r)/ 3.0 len = raw_input("enter the len:") print "the area of suqare is: %f" % suqare_area(len)
print "the volume of square is: %f" % suqare_volume(len) r = raw_input("enter the r:")
print "the area of the circle is: %f" % circle_area(r)
print "the volume of spere is: %f" % spere_volum(r)
5-10
#5-10
def fah_to_centi(c):
return (c - 32)*(5/float(9)) c = raw_input("enter the fahrenheit:") print "the centi degrees is: %f" % fah_to_centi(float(c))
5-11
#5-11
def odd_num(num):
return num %2 == 1 def aliquant(n1, n2):
return n1 % n2 == 0 for e in range(0, 20):
if not odd_num(e):
print "even number:%d" % e for e in range(0, 20):
if odd_num(e):
print "odd number: %d" % e n1 = raw_input("enter the first number:")
n2 = raw_input("enter the second number:")
if aliquant(int(n1), int(n2)):
print"aliquant"
else:
print"not aliquant"
5-12
#5-12
import sys print sys.maxint
print -sys.maxint - 1 print sys.float_info.max
print sys.float_info.min
5-13
#5-13 def hm_to_m(str):
a = str.split(':')
return int(a[0])* 60 + int(a[1]) hm = raw_input("enter the time:") print "the currents mins is: %d" % hm_to_m(hm)
5-14
#5-14
def year_rate():
return (1+0.0385)** 365 - 1.0 print year_rate()
5-15
#5-15
def GCD(m,n):
if m%n == 0:
return n
else:
return GCD(n, m%n) def LCM(m, n):
return m*n/GCD(m, n) m = int(raw_input("enter m:"))
n = int(raw_input("enter n:")) print"the GCD of %d, %d is: %d" % (m, n, GCD(m, n))
print"the LCM of %d, %d is: %d" % (m, n, LCM(m, n))
5-17
#5-17
import random N = random.randint(2, 100)
a = [] def show_all(a):
for i in range(len(a)):
print a[i] for i in range(0, N-1):
tmp = random.randint(2, 2**31)
a.append(tmp)
print "Original:"
show_all(a) for i in range(0, N-1):
for j in range(i, N-1):
if a[i] > a[j]:
a[i], a[j] = a[j], a[i] print"New arrary:"
show_all(a)
python 核心编程课后练习(chapter 5)的更多相关文章
- python 核心编程课后练习(chapter 6)
6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...
- python 核心编程课后练习(chapter 3)
3-8 #3-8 "makeTextFile.py -- create text file" import os ls = os.linesep #get filename fna ...
- python 核心编程课后练习(chapter 2)
2-4 #2-4(a) print "enter a string" inputstring = raw_input() print"the string is: &qu ...
- Python核心编程课后习题-第六章
1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...
- Python 核心编程 课后习题 第五章
2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...
- Python核心编程 课后练习 第二章
2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...
- python核心编程(第二版)习题
重新再看一遍python核心编程,把后面的习题都做一下.
- Python核心编程这本书的一些错误
<Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...
- Python核心编程-描述符
python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...
随机推荐
- Python列表
列表不同于字符串和元组:列表是可变的--可以改变列表的内容 1.列表函数 1.list(x)函数(其实是一种类型,而不是一个真正意义上的函数) 转化为列表,其中x可以是其他序列 可以用''.join( ...
- 转自一个CG大神的文章
<如何学好游戏3D引擎编程>此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我,这样才能攀登到游戏技术的最高峰 ——阿哲VS自 ...
- js中cookie的使用详细分析
JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...
- ApiResponse 在 Swagger 1 和Swagger 2中的不同
在1中随意写code , 在 2中 会提示 com.google.common.util.concurrent.UncheckedExecutionException: java.lang.Illeg ...
- mybatis 与 xml
mybatis的两大重要组件:配置和映射文件,都是可以通过xml配置的(新版本新增了注解的方式配置Mapper),下面来解析下mybatis是怎么做的 其中,关于配置文件解析的主要是在这个类XMLCo ...
- 解除svn版本控制
步骤1.去除目录下的所有.svn文件夹:两种方式: 方法1:搜索目录下所有.svn文件,删除: 方法2:复制下列文字到txt中,然后把扩展名改为reg,放到需要去除.svn的目录中,双击运行注册表即可 ...
- centos nginx server_name 配置域名访问规则
今天配置Server_name时,希望禁用一些域名,应为这些域名我想让通过另外一个Server配置 server_name "~^((\w*[^w]{1}\w*)|w{1,2})\.hell ...
- dotNet下的一套解决方案
很久没在博客园写文章了,打算把一直由自己一个人写的一整套系统开放出来,今天先放一些截图及可以演示的地址! 这套系统包含数据层(HB.Data).计划任务(HB.PlanTask).日志系统(HB.Lo ...
- day24:面向对象设计与面向对象编程、类和对象
一.三大编程范式: 面向过程: 面向函数: 面向对象: 二.程序的进化论: 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 2.从上述的指令中提取重复的代码块或逻辑,组织到一起(比方说,你定 ...
- 【maven】之配置开发,测试,正式环境pom.xml文件
在进行web程序开发,如果项目组没有使用自动化发布工具(jenkins + maven + svn + tomcat ),我们一般会使用maven的热部署来完成发布,在部署的过程中我们开发,测试,生产 ...