免费注册 查看新帖 |

Chinaunix

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

生成器怎么退出 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-25 10:40 |显示全部楼层 |倒序浏览
  1. def choose():
  2.     print "please send your choose('1'and'2','q' to quit):"
  3.     while True:
  4.         choose=yield
  5.         if choose=='1':
  6.             print 'you have chosen 1,function_1'
  7.         elif choose=='2':
  8.             print 'you have chosen 2,function_2'
  9.         elif choose=='q':
  10.             print 'quit'
  11.             break
  12.         else:
  13.             print "please choose again('1'and'2')"
  14. c=choose()
  15. c.next()
  16. c.send('2')
  17. c.send('q')
  18. c.send('3')
  19. c.send('5')
复制代码
出现错误:
Traceback (most recent call last):
  File "C:\Documents and Settings\dsf\My Documents\Aptana Studio 3 Workspace\advance\src\generator_choose.py", line 17, in <module>
    c.send('q')
StopIteration

怎么改让程序退出不报错

论坛徽章:
0
2 [报告]
发表于 2011-08-25 12:35 |显示全部楼层
去掉break那还是可以send

论坛徽章:
0
3 [报告]
发表于 2011-08-25 17:52 |显示全部楼层
具体怎么捕捉啊,c.send('q')才会异常,能不能在方法里捕捉啊

论坛徽章:
0
4 [报告]
发表于 2011-08-25 17:53 |显示全部楼层
回复 6# phantomCu


    是python2.5的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP