python代碼出錯

          Posted on 2008-08-15 17:41 Robert Su 閱讀(1290) 評論(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

          主站蜘蛛池模板: 红桥区| 中超| 临朐县| 内乡县| 清河县| SHOW| 苏尼特左旗| 普兰店市| 昌乐县| 马鞍山市| 德安县| 惠东县| 从江县| 浦城县| 天气| 南乐县| 金门县| 兴隆县| 炉霍县| 固阳县| 紫金县| 天台县| 得荣县| 托克托县| 盐边县| 合阳县| 章丘市| 榆社县| 威海市| 镇远县| 安多县| 资溪县| 宜昌市| 毕节市| 汽车| 客服| 二手房| 息烽县| 姜堰市| 象山县| 奉化市|