python代码出错

Posted on 2008-08-15 17:41 Robert Su 阅读(1204) 评论(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