《Python核心编程》中文第二版课后练习3.8答案

来源:互联网 发布:matlab 字符数组 编辑:程序博客网 时间:2024/04/30 03:43
3-1 变量在第一次被赋值时自动声明。在赋值时解释器会根据语法和右侧的操作数来决定新对象的类型。

3-2 函数总是返回一个值,显式return的值或者None。返回的值的类型也是动态确定。

3-3 因为变量名__xxx__对Python来说有特殊含义,对于普通的变量应当避免这种命名风格。

3-4 可以,使用";"

3-5 可以,使用"\"或者括号

3-6
(a)x = 1, y = 2, z = 3
(b)z = 2, x = 3, y = 1

3-7 40XL, $aving$, 0x40L, thisIsn'tAVar, big-daddy, 2hot2touch, counter-1不合法。不是以字母或者下划线开头,或者有非字母下划线数字的字符。print, if是关键字。

3-10
用异常处理取代对os.path.exists()的调用:
import osls = os.linesepwhile True:    fname = raw_input('input a filename:')#    if os.path.exists(fname):#        print "Error: '%s' already exists" % fname#    else:#        break    try:        fobj = open(fname, 'r')    except IOError, e:        break;    else:        print "Error: '%s' already exists" % fname        fobj.close()all = []print "\nEnter lines ('.' by itself to quit).\n"while True:    entry = raw_input('>')    if entry == '.':        break    else:        all.append(entry)fobj = open(fname, 'w')fobj.writelines(['%s%s' % (x, ls) for x in all])fobj.close()print 'Done!'



用os.path.exists()取代异常处理方法:
import osfname = raw_input('Enter filename:')print#try:#    fobj = open(fname, 'r')#except IOError, e:#    print '*** file open error:', e#else:#    for eachLine in fobj:#        print eachLine,#    fobj.close()if os.path.exists(fname):    fobj = open(fname, 'r')    for eachLine in fobj:        print eachLine,    fobj.close()else:    print 'this file not exists'



3-11
import osfname = raw_input('Enter filename:')print#try:#    fobj = open(fname, 'r')#except IOError, e:#    print '*** file open error:', e#else:#    for eachLine in fobj:#        print eachLine,#    fobj.close()if os.path.exists(fname):    fobj = open(fname, 'r')    for eachLine in fobj:        print eachLine.strip()    fobj.close()else:    print 'this file not exists'



3-12
"""to read or make a file"""import osdef makeTextFile():    '''make a file'''    ls = os.linesep    while True:        fname = raw_input('input a filename:')    #    if os.path.exists(fname):    #        print "Error: '%s' already exists" % fname    #    else:    #        break        try:            fobj = open(fname, 'r')        except IOError, e:            break;        else:            print "Error: '%s' already exists" % fname            fobj.close()    all = []    print "\nEnter lines ('.' by itself to quit).\n"    while True:        entry = raw_input('>')        if entry == '.':            break        else:            all.append(entry)    fobj = open(fname, 'w')    fobj.writelines(['%s%s' % (x, ls) for x in all])    fobj.close()    print 'Done!'def readTextFile():    '''read a file'''    fname = raw_input('Enter filename:')    print    #try:    #    fobj = open(fname, 'r')    #except IOError, e:    #    print '*** file open error:', e    #else:    #    for eachLine in fobj:    #        print eachLine,    #    fobj.close()    if os.path.exists(fname):        fobj = open(fname, 'r')        for eachLine in fobj:            print eachLine.strip()        fobj.close()    else:        print 'this file not exists'def main():    '''main menu'''    while True:        print '1.Read a file'        print '2.Make a file'        print 'x.exit'        myStr = raw_input('input your choice:')        if myStr == '1':            readTextFile()        elif myStr == '2':            makeTextFile()        elif myStr == 'x':            breakif __name__ == '__main__':    main()



3-13
Windows下curses模块貌似无法正常使用……
迂回的实现思路是将文件内容按行读取到一个列表里,
然后让用户修改每一行,
最后让用户选择是否保存,
保存则重写文件,
不保存则退出。
原创粉丝点击