`

python 核心编程 第三章 习题答案

阅读更多
3–1. 标识符。为什么 Python 中不需要变量名和变量类型声明?
答:python 是动态类型语言,在赋值时,根据"=" 右边动态确定变量类型,不需要特别的声明,在运行时,才会调用类型使用。变量在第一次被赋值时自动声明,。Python 语言中, 对象的类型和内存占用都是运行时确定的。尽管代码被编译成字节码,Python 仍然是一种解释型语言

3–2. 标识符。为什么 Python 中不需要声明函数类型?
答:函数没有定义返回的数据类型。 Python不需要提定返回值的数据类型;甚至不需要指定是否有返回值。实际上,每个Python函数都返回一个值;如果函数执行过return语句,它将返回指定的值,否则将返回None(Python 的空值)。

3–3. 标识符。为什么应当避免在变量名的开始和和结尾使用双下划线?
答:__xxxx__ 这样的变量类型在python中有特殊意义,表示 "系统定义名字"  ,这样的做的目的减少自定义变量覆盖系统定义变量,从而导致脚本运行出错。

3–4. 语句。在 Python 中一行可以书写多个语句吗?
答:可以,使用";“ 隔开即可

3–5. 语句。在 Python 中可以将一个语句分成多行书写吗?
答:可以,使用”\“ 即可实现分多行书写

3–6. 变量赋值
(a)赋值语句 x, y, z = 1, 2, 3 会在 x、y、z 中分别赋什么值?
(b)执行 z, x, y = y, z, x 后,x、y、z 中分别含有什么值?

答: x,y,z,分别是 1,2,3  在执行b)后 三值互换
x=3,y=2,z=1

3–7. 标识符。下面哪些是 Python 合法的标识符?如果不是,请说明理由!在合法的标
识符中,哪些是关键字?
int32  40XL  $aving$  printf  print
_print  this  self  __name__ 0X40L
bool  true  big-daddy 2hot2touch type
thisIsn'tAVar thisIsAVar R_U_Ready Int  True
if  do  counter-1 access  -

答案
Python标识符字符串规则和其他大部分用C编写的高级语言相似:
第一个字符必须是字母或下划线“_”;剩下的字符可以是字母数字或下划线;大小写敏感。
int32、printf、_print、this、self、__name__、bool、true、type、thisIsAVar、R_U_Ready、Int、True、do、access是Python合法的标识符。
print、if、是Python合法的标识符且是关键字。

40XL、$aving$、0X40L、big-daddy、2hot2touch、thisIsn'tAVar、counter-1、-不是Python合法的标识符。

下面的问题涉及了 makeTextFile.py 和 readTextFile.py 脚本。

3–8. Python 代码。将脚本拷贝到您的文件系统中,然后修改它。可以添加注释,修改
提示符(‘>’太单调了)等等,修改这些代码,使它看上去更舒服。
答:动手吧,注意缩进,尽量使用空格

3–9.移植。 如果你在不同类型的计算机系统中分别安装有Python, 检查一下,
os.linesep 的值是否有不同。 记下操作系统的类型以及 linesep 的值。
答:导入模板 import os .多试试

3–10.异常。使用类似readTextFile.py 中异常处理的方法取代 readTextFile.py
makeTextFile.py 中 对 os.path.exists()的调用。反过来, 用os.path.exists() 取 代
readTextFile.py 中的异常处理方法。
答:动手

3–11.
字符串格式化 不再抑制 readTextFile.py 中 print 语句生成的 NEWLINE 字符,修改你的
代码,在显示一行之前删除每行末尾的空白。这样,你就可以移除 print 语句末尾的逗号了。
提示: 使用字符串对象的 strip()方法
#!/usr/bin/env Python

'readTextFile.py -- read and display text file'

# get filename
fname=raw_input('Enter filename: ')
print

# attempt to open  file for reading
try:
   fobj = open(fname, 'r')
except IOError, e:
   print "*** file open error:", e
else:
   # display contents to the screen
   for eachLine in fobj:
       #print eachLine,
       print eachLine.strip()
fobj.close()

3–12. 合并源文件。将两段程序合并成一个,给它起一个你喜欢的名字,比方
readNwriteTextFiles.py。让用户自己选择是创建还是显示一个文本文件。
答:如下
==========================================================
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
ls = os.linesep

def Write_file():
    #'makeTextFile.py -- create text file'
    # get filename
    while True:
        fname = raw_input('enter a file name')
        if os.path.exists(fname):
           print "ERROR: '%s' already exists" % fname
        else:
           break
       
    # get file content ()text) lines
    all = []
    print "\nEnter lines ()'.' by itself to quit).\n"
    
    # loop until user terminates input
    while True:
        entry = raw_input('> ')
        if entry == '.':
           break
        else:
          all.append(entry)
        # write lines to file with proper line-ending
          fobj = open(fname, 'w')
          fobj.writelines(['%s%s' % (x, ls) for x in all])
          fobj.close()
          print 'DONE!'

def READ_FILE():

#'readTextFile.py -- read and display text file'

# get filename
    fname=raw_input('Enter filename: ')
    print
   
    # attempt to open  file for reading
    try:
       fobj = open(fname, 'r')
    except IOError, e:
       print "*** file open error:", e
    else:
       # display contents to the screen
       for eachLine in fobj:
           #print eachLine,
           print eachLine.strip()
    fobj.close()

if __name__=="__main__":
   while True:
       print """please select target:
           1) read txtfile
           2) create a textfile
           3) quit
           """
       getcode=raw_input("which one: ")
       if getcode=='1':
          READ_FILE()
       elif getcode=='2':
          Write_file()
       elif getcode=='3':
          break
       else:
          print("check you choice,repeat")
      
================================================================
3–13. 添加新功能。将你上一个问题改造好的 readNwriteTextFiles.py 增加一个新功
能:允许用户编辑一个已经存在的文本文件。 你可以使用任何方式,无论是一次编辑一行,还
是一次编辑所有文本。需要提醒一下的是,一次编辑全部文本有一定难度,你可能需要借助 GUI
工具包或一个基于屏幕文本编辑的模块比如 curses模块。要允许用户保存他的修改(保存到
文件)或取消他的修改(不改变原始文件),并且要确保原始文件的安全性(不论程序是否正
常关闭)。

答:使用了Tkinter  ,platform
注:初学者可能不容易掌握

感谢作者:ToughGuy
01 #!/usr/bin/env python
02 #-*- coding: utf-8 -*-
03 #=============================================================================
04 #     FileName:
05 #         Desc:
06 #       Author: ToughGuy
07 #        Email: wj0630@gmail.com
08 #     HomePage: http://www.techzhai.net/
09 #      Version: 0.0.1
10 #   LastChange: 2013-02-20 14:52:11
11 #      History:
12 #=============================================================================
13
14 from Tkinter import *
15 import tkMessageBox,tkFileDialog
16 import platform
17
18 # nl = os.linesep
19
20 def openfile():
21     global filename             # 使用global声明为全局变量,方便后边的程序调用
22     systype = platform.system() # 判断系统类型
23     if systype == 'windows':
24         basedir = 'c:\\'
25     else:
26         basedir = '/'
27     filename = tkFileDialog.askopenfilename(initialdir=basedir)
28     try:
29         fobj_r = open(filename, 'r')
30     except IOError, errmsg:
31         print '*** Failed open file:', errmsg
32     else:
33         editbox.delete(1.0, END)
34         for eachline in fobj_r:
35             editbox.insert(INSERT, eachline)
36         fobj_r.close()
37
38 def savefile():
39     save_data = editbox.get(1.0, END)
40     try:
41         fobj_w = open(filename, 'w')
42         fobj_w.writelines(save_data.encode('utf-8')) # 感谢OSC-骠骑将军 指教
43         fobj_w.close()
44         tkMessageBox.showinfo(title='提示',
45                 message='保存成功')
46     except IOError, errmsg:
47         tkMessageBox.showwarning(title='保存失败', message='保存出错    ')
48         tkMessageBox.showwarning(title='错误信息', message=errmsg)
49     except NameError:
50         tkMessageBox.showwarning(title='保存失败', message='未打开文件')
51 def showlinenum():
52     tkMessageBox.showinfo(title='提示',
53             message='这个功能作者现在不会写,放这里装饰用的.')
54 def destroy_ui(ui):
55     ui.destroy()
56
57 def aboutauthor():
58     author_ui = Toplevel()
59     author_ui.title('关于')
60     author_ui.geometry('200x80')
61     about_string = Label(author_ui,
62             text="作者: ToughGuy\n\n主页: http://www.techzhai.net/")
63     confirmbtn = Button(author_ui, text='确定',
64             command=lambda:destroy_ui(author_ui))
65     about_string.pack()
66     confirmbtn.pack()
67     # author_ui.mainloop()
68
69 def CreateMenus():
70     # 初始化菜单
71     Menubar = Menu(root)
72
73     # 创建文件菜单
74     filemenu = Menu(Menubar, tearoff=0)
75     filemenu.add_command(label='打开文件', command=openfile)
76     filemenu.add_command(label='保存文件', command=savefile)
77     filemenu.add_command(label='退出', command=lambda:destroy_ui(root))
78     Menubar.add_cascade(label='文件', menu=filemenu)
79
80     # 创建编辑菜单
81     editmenu = Menu(Menubar, tearoff=0)
82     editmenu.add_command(label='显示行号', command=showlinenum)
83     Menubar.add_cascade(label='编辑', menu=editmenu)
84
85     # 创建帮助菜单
86     helpmenu = Menu(Menubar, tearoff=0)
87     helpmenu.add_command(label='关于作者', command=aboutauthor)
88     Menubar.add_cascade(label='帮助', menu=helpmenu)
89     root.config(menu=Menubar)
90
91 root = Tk()
92 root.title('文本编辑器')
93 root.geometry('500x400')
94 CreateMenus()
95 editbox = Text(root, width=70, height=25, bg='white')
96 editbox.pack(side=TOP, fill=X)
97 root.mainloop()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics