Python核心编程 练习
2–9.
循环和运算符 创建一个包含五个固定数值的列表或元组,输出他们的平均值。本练习的难点之一是通过除法得到平均值。 你会发现整数除会截去小数,因此你必须使用浮点除以得到更精确的结果。 float()内建函数可以帮助你实现这一功能。
>>> def a(): #function1
i=0
sum=0
while i<5:
i=i+1
num=input('enter %d:'%(i))
sum=sum+float(num)
avg=sum/5
print("avg is %f"%(avg)) >>> a()
enter 1:1
enter 2:2
enter 3:3
enter 4:4
enter 5:56
avg is 13.200000
>>> def a(): #function2
print('input five number:')
i=0
sum=0
while i<5:
i=i+1
num=input()
sum=sum+float(num)
avg=sum/5
print("avg is %f"%(avg)) >>> a()
input five number:
3
4
67
23
564.34
avg is 132.268000
2–10.
带循环和条件判断的用户输入 使用raw_input()函数来提示用户输入一个1 和100 之间的数,如果用户输入的数满足这个条件,显示成功并退出。否则显示一个错误信息然后再次提示用户输入数值,直到满足条件为止。
def a():
i=0
while i==0:
try:
a=input('please enter a number between 1 and 100:')
num=float(a)
if num>1 and num<100:
print('you entered the number %f'%(num))
break
else: print("number needs 1~100!")
except ValueError:
print("please input number!") >>> a()
please enter a number between 1 and 100:sad
please input number!
please enter a number between 1 and 100:100
number needs 1~100!
please enter a number between 1 and 100:23
you entered the number 23.000000
2–11.
带文本菜单的程序 写一个带文本菜单的程序,菜单项如下(1)取五个数的和 (2) 取五个数的平均值....(X)退出。由用户做一个选择,然后执行相应的功能。当用户选择退出时程序结束。这个程序的有用之处在于用户在功能之间切换不需要一遍一遍的重新启动你的脚本。(这对开发人员测试自己的程序也会大有用处)
def function():
print('input 5 numbers:')
i=0
sum=0
while i<5:
i=i+1
num=input()
sum+=float(num)
while 1:
print('1.sum') #这几个print可以放到while 1外面去,表示不需要每次选择前都会显示选项
print('2.average')
print('3.exit')
select=int(input('you select:'))
if select==1:
print('the sum is:'),
print(sum)
elif select==2:
avg=sum/5
print('the average is:'),
print(avg)
else:break
3-13
这个题目实在是太折腾了,将新建文本、查看文本、新增文本内容写到一起,然后带文本菜单。
不过还是搞不清楚两点:1、怎么删掉多出来的空格;2、怎么修改完整的文件,包括删除语句、修改原有语句
def b():
#!/usr/bin/env python
'makeTextFile.py--creat text file'
import os
ls=os.linesep
#get filename
while True:
print("1.creat a file");print("2.read a file");print("3.modify a file");print("4.exit")
select=int(input('I want:'))
if select==1:
while True:
fname=input('enter a filename:')
if os.path.exists(fname):
print("Error:'%s' already exists"%(fname))
else:break
#get file content (text) lines
allw=[]
print("\n Enter lines('.' by itself to quit).\n")#loop until user terminates input
while True:
entry=input('>')
if entry=='.':
break
else:
allw.append(entry)
#write lines to file with proper line-ending
fobjw=open(fname,'w')
fobjw.writelines(['%s%s'%(x,ls)for x in allw])
fobjw.close()
print('Seccess new!')
if select==2:
while True:
fname=input('enter a filename:')
if os.path.exists(fname):
fobjr=open(fname,'r')
for filetxt in fobjr:
print(filetxt)
fobjr.close()
break
else:
print('%s file is not exist'%(fname))
print('Seccess read!')
if select==3:
while True:
fname=input('enter a filename:')
if os.path.exists(fname):
break
else:
print('%s file is not exist'%(fname))
fobjr=open(fname,'r')
all=fobjr.readlines()
print("\n Enter lines('.' by itself to quit).\n")
while True:
entry=input('+')
if entry=='.':
break
else:all.append(entry)
fobj=open(fname,'w')
fobj.writelines(['%s%s'%(x,ls)for x in all])
fobj.close()
fobjr.close()
print("Seccess modify")
if select==4:break
5-4 取余。判断给定年份是否是闰年。使用下面的公式:一个闰年就是指它可以被4 整除,但不能被100 整除, 或者它既可以被4 又可以被100 整除。比如 1992,1996 和2000 年是闰年,但1967 和1900 则不是闰年。下一个是闰年的整世纪是 2400 年。
def Leap():
x=int(input('enter;'))
a=x%4
b=x%100
c=x%400
if c==0:
m='is'
elif a==0:
if b==0:
m='is not'
else:
m='is'
else:
m='is not'
print('%d'%x, m,'Leap Year')
5-5 取余。取一个任意小于1 美元的金额,然后计算可以换成最少多少枚硬币。硬币有1美分,5 美分,10 美分,25 美分四种。1 美元等于100 美分。举例来说,0.76 美元换算结果应该是 3 枚25 美分,1 枚1 美分。类似76 枚1 美分,2 枚25 美分+2 枚10 美分+1 枚5 美分+1枚1 美分这样的结果都是不符合要求的。
def yuan():
x=float(input('enter a number of dollor:'))
z=100*x
(a,b)=divmod(z,25)
if a==0:
n1=0
(c,d)=divmod(z,10)
if c==0:
n2=0
(e,f)=divmod(z,5)
if e==0:
n3=0
n4=f
else:
n3=e
n4=f
else:
n2=c
(e,f)=divmod(d,5)
if e==0:
n3=0
n4=f
else:
n3=e
n4=f
else:
n1=a
(c,d)=divmod(b,10)
if c==0:
n2=0
(e,f)=divmod(b,5)
if e==0:
n3=0
n4=f
else:
n3=e
n4=f
else:
n2=c
(e,f)=divmod(d,5)
if e==0:
n3=0
n4=f
else:
n3=e
n4=f
print(x,'equals %d $25 %d $10 %d $5 %d $1'%(n1,n2,n3,n4))
Python核心编程 练习的更多相关文章
- python核心编程(第二版)习题
重新再看一遍python核心编程,把后面的习题都做一下.
- Python核心编程这本书的一些错误
<Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...
- Python核心编程-描述符
python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...
- Python核心编程-闭包
百度搜了一下闭包的概念:简而言之,闭包的作用就是在外部函数执行完并返回后,闭包使得收机制不会收回函数所占用的资源,因为内部函数的执行需要依赖外函数中的变量.这是对闭包作用的非常直白的描述,不专业也不严 ...
- python核心编程第二版笔记
python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成 ...
- 学习《Python核心编程》做一下知识点提要,方便复习(一)
学习<Python核心编程>做一下知识点提要,方便复习. 计算机语言的本质是什么? a-z.A-Z.符号.数字等等组合成符合语法的字符串.供编译器.解释器翻译. 字母组合后产生各种变化拿p ...
- python核心编程--笔记
python核心编程--笔记 的解释器options: 1.1 –d 提供调试输出 1.2 –O 生成优化的字节码(生成.pyo文件) 1.3 –S 不导入site模块以在启动时查找pyt ...
- Python核心编程第二版(中文).pdf 目录整理
python核心编程目录 Chapter1:欢迎来到python世界!-页码:7 1.1什么是python 1.2起源 :罗萨姆1989底创建python 1.3特点 1.3.1高级 1.3.2面向 ...
- Python核心编程
对<Python核心编程>的褒奖" The long-awaited second edition of Wesley Chun's Core PythonProgramming ...
- python经典书籍推荐:Python核心编程
作者:熊猫烧香 链接:www.pythonheidong.com/blog/article/27/ 来源:python黑洞网 对<Python核心编程>的褒奖 “ The long-awa ...
随机推荐
- JAVA中的继承和覆盖
java里面的继承是子类继承父类的一些方法和属性(除private属性和方法之外):对于父类的私有属性和方法子类是没有继承的.可是要想子类也能訪问到父类的私有属性,必须给私有属性以外界訪问的方法接口. ...
- PHP安全编程:会话数据注入 比会话劫持更强大的攻击(转)
一个与会话暴露类似的问题是会话注入.此类攻击是基于你的WEB服务器除了对会话存储目录有读取权限外,还有写入权限.因此,存在着编写一段允许其他用户添加,编辑或删除会话的脚本的可能.下例显示了一个允许用户 ...
- Java EE 7 / JAX-RS 2.0: Simple REST API Authentication & Authorization with Custom HTTP Header--reference
REST has made a lot of conveniences when it comes to implementing web services with the already avai ...
- java mysql驱动
mysql驱动方式有三种, 1.第一种是先把jar包放在项目的目录下,通过添加jar包,是使用相对地址的,这样把项目复制到其它电脑也可以用 2.第二种方法是导入外部的jar包,是绝对地址,如果项目要复 ...
- [转] socket异步编程--libevent的使用
这篇文章介绍下libevent在socket异步编程中的应用.在一些对性能要求较高的网络应用程序中,为了防止程序阻塞在socket I/O操作上造成程序性能的下降,需要使用异步编程,即程序准备好读写的 ...
- Java笔试题二:读程序
public class SopResult { public static void main(String[] args) { int i = 4; System.out.println(&quo ...
- IDL绘制黑体辐射曲线
普朗克定律是热红外遥感中常常使用的三大定律之一,描述了黑体辐射能量的情况.绝对黑体的辐射光谱对于研究一切物体的辐射规律具有根本的意义.1900年普朗克引进量子概念,将辐射当做不连续的量子发射,成功地从 ...
- SPOJ 4053 - Card Sorting 最长不下降子序列
我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...
- datagrid加下拉列表dropdownlist
datagrid中代码: <asp:datagrid id="dgList" runat="server" ItemStyle-HorizontalAli ...
- (转)SQL Server中使用convert进行日期转换
原文链接:http://www.cnblogs.com/weiqt/articles/1826847.html SQL Server中使用convert进行日期转换 一般存入数据库中的时间格式为yyy ...