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代碼出錯  回復  更多評論   

          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

          主站蜘蛛池模板: 陈巴尔虎旗| 内乡县| 全椒县| 田东县| 永善县| 福鼎市| 桃园市| 桦南县| 醴陵市| 东乡族自治县| 安多县| 涿鹿县| 龙井市| 怀宁县| 中方县| 沂水县| 巴东县| 略阳县| 呼伦贝尔市| 怀宁县| 徐汇区| 凉山| 二手房| 德庆县| 定南县| 花莲市| 杂多县| 林西县| 北流市| 搜索| 雅安市| 原平市| 长海县| 尖扎县| 大宁县| 平江县| 仲巴县| 翁牛特旗| 盐源县| 台东县| 迁安市|