python代碼出錯

          Posted on 2008-08-15 17:41 Robert Su 閱讀(1291) 評論(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代碼出錯  回復  更多評論   

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

          # re: python代碼出錯  回復  更多評論   

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

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


          網站導航:
           

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

          Copyright © Robert Su

          主站蜘蛛池模板: 米脂县| 双桥区| 永靖县| 集安市| 乐业县| 灵台县| 天祝| 陇川县| 晋宁县| 翁源县| 封丘县| 抚松县| 博乐市| 沂水县| 新疆| 梧州市| 云林县| 平舆县| 张家川| 公主岭市| 布尔津县| 通州市| 昂仁县| 宁陕县| 延边| 克什克腾旗| 芜湖县| 资溪县| 南城县| 微山县| 建宁县| 无锡市| 苏尼特左旗| 腾冲县| 湖南省| 汉川市| 枞阳县| 焉耆| 朝阳市| 福海县| 旺苍县|