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

          主站蜘蛛池模板: 新巴尔虎右旗| 登封市| 大化| 扶风县| 淳安县| 古浪县| 酉阳| 富源县| 奉化市| 石城县| 油尖旺区| 宜黄县| 新建县| 佛冈县| 资兴市| 绥化市| 辉南县| 育儿| 剑阁县| 习水县| 长海县| 黄大仙区| 灵川县| 明溪县| 芷江| 蕉岭县| 健康| 抚州市| 宜宾市| 邯郸市| 海淀区| 开原市| 老河口市| 柯坪县| 苏州市| 曲阜市| 阳朔县| 仁寿县| 横山县| 嘉黎县| 和田市|