免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4119 | 回复: 2
打印 上一主题 下一主题

python 嵌套语句 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-13 11:14 |只看该作者 |倒序浏览
本帖最后由 liumilan2009 于 2013-09-13 11:24 编辑

1
  2 f_less = open('less20.txt','w')
  3 f_more = open('more20.txt','w')
  4
  5 for line in open("test"):
  6 #   line = line.strip()
  7    if line.find("start to print")>0 :
  8      num = 0
  9      word = ""
10      f_less.write(line)
11      f_more.write(line)
12    elif  line.find("end to print")>0:
13      if( num <20):
14         f_less.write(word)
15         f_less.write(line)
16      else:
17         f_more.write(word,line)
18    else:
19      num=num+1;
20      word = word+line
21
这时候报 python test.py
  File "test.py", line 16
    else:
        ^
IndentationError: unindent does not match any outer indentation level

elif语句后面不能再有else语句吗?怎么改啊?

论坛徽章:
0
2 [报告]
发表于 2013-09-13 11:39 |只看该作者
去看看缩进,应该是缩进的问题

你的16行的else是对应7行的if,还是5行的else,弄好缩进已经就行了

论坛徽章:
0
3 [报告]
发表于 2013-09-13 11:40 |只看该作者
目测是缩进问题。
请看错误提示:IndentationError: unindent does not match any outer indentation level.
翻译过来是:缩进错误:未匹配到任何缩进对应格式。(大意是这样。)

建议你改进缩进试试。

elif中嵌套else是可以实现的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP