python代碼出錯

          Posted on 2008-08-15 17:41 Robert Su 閱讀(1302) 評論(2)  編輯  收藏
          111
          import sys
          def readfile(filename):
                  
          '''Print a file the standard output.'''
                  f 
          = file(filename)
                  
          while True:
                          line 
          = f.readline()
                          
          if len(line) == 0:
                                  
          break
                          
          print line, #notice comma
                  f.close()

          if len(sys.argv) < 2:
                  
          print 'No action specified.'
                  sys.exit()

          if sys.argv[1].startswith('--'):
                  option 
          = sys.argv[1][2:]
                  
          if option == 'verison':
                          
          print 'Version 1.2'
                  
          elif option == 'help':
                          
          print '''\
          This program prints files to the standard output.
          Any number of files can be specified.
          Options include:
                  --version : Print the version number
                  --help    : Display this help
          '''
                          
          else:
                          
          print 'Unknown option.'
                          sys.exit()
          else:
                  
          for filename in sys.argv[1:]:
                          readfile(filename)
          運行提示:
          D:\python>python cat.py
            File "cat.py", line 27
              else:
                 ^
          SyntaxError: invalid syntax

          誰知道這個哪里出錯了啊

          Feedback

          # re: python代碼出錯  回復(fù)  更多評論   

          2008-08-16 14:03 by 孟慶超
          else的縮進有問題,應(yīng)該與上面的elif對齊,python對縮進是敏感的

          # re: python代碼出錯  回復(fù)  更多評論   

          2008-08-19 17:08 by Robert Su
          @孟慶超
          謝謝您啦

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          posts - 103, comments - 104, trackbacks - 0, articles - 5

          Copyright © Robert Su

          主站蜘蛛池模板: 锡林浩特市| 申扎县| 石首市| 峨边| 雷山县| 安龙县| 衡山县| 南开区| 石城县| 白朗县| 额尔古纳市| 云浮市| 达尔| 广西| 江口县| 江油市| 宜黄县| 绥芬河市| 淄博市| 奉化市| 南投县| 大宁县| 黎平县| 正阳县| 淮安市| 赣榆县| 昌平区| 六盘水市| 鹤壁市| 舒兰市| 岫岩| 衢州市| 蓝山县| 修水县| 乐至县| 关岭| 博湖县| 泰州市| 赣榆县| 吉水县| 北碚区|