workspace

努力奋斗——I believe I can fly

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  82 随笔 :: 5 文章 :: 64 评论 :: 0 Trackbacks
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

谁知道这个哪里出错了啊
posted on 2008-08-15 17:41 Robert Su 阅读(151) 评论(2)  编辑  收藏

评论

# re: python代码出错 2008-08-16 14:03 孟庆超
else的缩进有问题,应该与上面的elif对齐,python对缩进是敏感的  回复  更多评论
  

# re: python代码出错 2008-08-19 17:08 Robert Su
@孟庆超
谢谢您啦  回复  更多评论
  


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交