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 ...
随机推荐
- PHP安全编程:网站安全设计的一些原则(转)
深度防范 深度防范原则是安全专业人员人人皆知的原则,它说明了冗余安全措施的价值,这是被历史所证明的. 深度防范原则可以延伸到其它领域,不仅仅是局限于编程领域.使用过备份伞的跳伞队员可以证明有冗余安全措 ...
- 网络接口 使用NSURLConnection完成Get和Post方法
网络接口 使用NSURLConnection完成Get和Post方法 什么是URL: URL就是统一资源定位器(UniformResourceLocator:URL).通俗地说,它是用来指出某一项信息 ...
- 使用downloadmanager调用系统的下载
/** * 文件名 UpdateDownload.java * 包含类名列表 com.issmobile.numlibrary.tool * 版本信息 版本号 * 创建日期 2014年7月14日 ...
- Centos ssh 登陆乱码解决办法
1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030"LANGUAGE="zh_CN.GB18030:zh_CN.GB23 ...
- Python之路,Day25-----暂无正在更新中
Python之路,Day25-----暂无正在更新中
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
java.lang.UnsupportedClassVersionError: Bad version number in .class file造成这种过错是ni的支撑Tomcat运行的JDK版本与 ...
- ASP.NET生成压缩文件(rar打包)
首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZi ...
- asp.net 中的那些编译错误(1):控件包含代码块(即<% ... %>),因此无法修改控件集合
在编译页面的时候出现:控件包含代码块(即 <% ... %>),因此无法修改控件集合错误 一般原因是: 在<head runat="server">< ...
- Html5 Canvas Text
html5 canvas中支持对text文本进行渲染;直接的理解就是把text绘制在画布上,并像图形一样处理它(可以加shadow.gradient.pattern.color fill等等):既然它 ...
- 解决 iOS View Controller Push/Pop 时的黑影
那么如何解决这个问题呢? 实际上很简单,如果这个 ViewController 是在 TabBarViewController 的 NavigationController 上 Push/Pop 的, ...