zoukankan      html  css  js  c++  java
  • 第三章-Python基础 课后答案

    3-1

    python是动态语言,它的对象的类型和内存都是运行时确定的;在创建新对象时,解释器会根据语法和右侧的操作数来决定新对象的类型。

    3-2

    python不用去声明函数的返回类型,是由其“若类型”的语言特性决定的。python是把储存的数据直接放到内存,然后再去用一个变量名引用这个数据。

    3-3

    因为python系统定义的名字是__xxx__,所以在变量名的开始和结尾使用双下划线容易引起混淆。

    3-4

    可以,但是要用";"分隔开。

    3-5

    可以,一行过长的语句可以使用反斜杠( )分解成几行;使用闭合操作符时,单一语句也可以跨多行,例如:在含有小括号、中括号、花括号时可以书写多行。另外就是三引号包括下的字符串也可以跨行书写。

    3-6

    (a) 1,2,3

    (b) 2,3,1

    3-7

    查文档

    3-8

    makeTextFile.py

     1 'makeTextFile.py -- create text file'
     2 
     3 import os
     4 ls = os.linesep
     5 
     6 #get filename
     7 while True:
     8 
     9     if os.path.exists('C:\Users\Administrator\Desktop\python\xue\weixing.txt'):
    10         print "ERROR: '%s' already exists" % ('C:\Users\Administrator\Desktop\python\xue\weixing.txt')
    11     else:
    12         break
    13 
    14 #get file content (text) lines
    15 all = []
    16 print "
    Enter lines ('.' by itself to quit).
    "
    17 
    18 # loop until user terminates input
    19 while True:
    20     entry = raw_input('input something you want to write: ')
    21     if entry == '.':
    22         break
    23     else:
    24         all.append(entry)
    25 
    26 # write lines to file with proper line-ending
    27 fobj = open('C:\Users\Administrator\Desktop\python\xue\weixing.txt', 'w')
    28 fobj.writelines(['%s%s' % (x, ls) for x in all])
    29 fobj.close()
    30 print 'DONE!'

    readTextFile.py

     1 'readTextFile.py -- read and display text file'
     2 
     3 # get filename
     4 fname = raw_input('Enter filename: ')
     5 print
     6 
     7 # attempt to open file for reading
     8 try:
     9     fobj = open(fname, 'r')
    10 except IOError, e:
    11     print "*** file open error:", e
    12 else:
    13     #display contents to the screen
    14     for eachLine in fobj:
    15         print eachLine,
    16     fobj.close()

    3-9

    os.linesep给出当前平台使用的行终止符

    windows平台使用 ' '

    linux平台使用 ' '

    mac平台使用 ' '

    3-10

    makeTextFile2.py

     1 import os
     2 ls = os.linesep
     3 
     4 #get filename
     5 while True:
     6     fname = raw_input("Enter file name:")
     7     try:
     8         open(fname, 'r')
     9         print "*** ERROR:'%s' already exists" % fname
    10     except IOError:
    11         break
    12         fname.close()
    13 
    14 # get file content (text) lines
    15 all = []
    16 print "
    Enter lines ('.' by itself to quit).
    "
    17 
    18 # loop until user terminates input
    19 while True:
    20     entry = raw_input("input something you want to write: ")
    21     if entry == '.':
    22         break
    23     else:
    24         all.append(entry)
    25 
    26 # write lines to file with proper line-ending
    27 fobj = open(fname, 'w')
    28 fobj.writelines(['%s%s' % (x, ls) for x in all])
    29 fobj.close()
    30 print 'DONE!'

    readTextFile2.py

     1 import os
     2 
     3 'readTextFile.py -- read and display text file'
     4 
     5 # get filename
     6 fname = raw_input('Enter filename: ')
     7 print
     8 
     9 # attempt to open file for reading
    10 if os.path.exists(fname):
    11     fobj = open(fname, 'r')
    12     for eachLine in fobj:
    13         print eachLine,
    14     fobj.close()
    15 else:
    16     print "Error: can't find the file!"
    17     

    3-11

     1 'readTextFile.py -- read and display text file'
     2 
     3 # get filename
     4 fname = raw_input('Enter filename: ')
     5 print
     6 
     7 # attempt to open file for reading
     8 try:
     9     fobj = open(fname, 'r')
    10 except IOError, e:
    11     print "*** file open error:", e
    12 else:
    13     #display contents to the screen
    14     for eachLine in fobj:
    15         print eachLine.strip()
    16     fobj.close()

    3-12

     1 import os
     2 ls = os.linesep
     3 
     4 def makeTextFile():
     5     while True:
     6         fname = raw_input("Enter file name:")
     7         try:
     8             open(fname, 'r')
     9             print "*** ERROR:'%s' already exists" % fname
    10         except IOError:
    11             break
    12             fname.close()
    13 
    14     # get file content (text) lines
    15     all = []
    16     print "
    Enter lines ('.' by itself to quit).
    "
    17 
    18     # loop until user terminates input
    19     while True:
    20         entry = raw_input("input something you want to write: ")
    21         if entry == '.':
    22             break
    23         else:
    24             all.append(entry)
    25 
    26     # write lines to file with proper line-ending
    27     fobj = open(fname, 'w')
    28     fobj.writelines(['%s%s' % (x, ls) for x in all])
    29     fobj.close()
    30     print 'DONE!'
    31 
    32 def readTextFile():
    33     fname = raw_input('Enter filename: ')
    34     print
    35 
    36     # attempt to open file for reading
    37     if os.path.exists(fname):
    38         fobj = open(fname, 'r')
    39         for eachLine in fobj:
    40             print eachLine,
    41         fobj.close()
    42     else:
    43         print "Error: can't find the file!"
    44 
    45 if __name__ == "__main__":
    46     while True:
    47         print """Choose one:
    48             1)makeTextFile
    49             2)readTextFile
    50             3)quit
    51         """
    52 
    53         getcode = raw_input("which you choose:")
    54         if getcode == "1":
    55             makeTextFile()
    56         elif getcode == "2":
    57             readTextFile()
    58         elif getcode == "3":
    59             break
    60         else:
    61             print "Please repeat one!"
  • 相关阅读:
    什么是 MyBatis?
    @RequestMapping 注解用在类上面有什么作用?
    如果你也用过 struts2.简单介绍下 springMVC 和 struts2 的区别有哪些?
    SpringMVC 流程?
    SpringMVC 工作原理?
    什么是 SpringMvc?
    依赖注入的三种实现方式?
    什么是IOC
    spring的作用
    什么是spring框架?
  • 原文地址:https://www.cnblogs.com/hell0x/p/5048717.html
Copyright © 2011-2022 走看看