try:
while True:
answer = raw_input("Type [Y]es to Exit :")
if answer.lower() in ["yes","y"]: raise StopIteration
print "Your answer is ", answer
except StopIteration:
print "Good Bye"
Type [Y]es to Exit :No
Your answer is No
Type [Y]es to Exit :Why
Your answer is Why
Type [Y]es to Exit :I Won't
Your answer is I Won't
Type [Y]es to Exit :Ok
Your answer is Ok
Type [Y]es to Exit :Yes
Good Bye
使用异常来中断循环。考虑下面的例子
实际上,您可以在多个级别组合多个出口。考虑下一个例子
^{pr2}$相关问题 更多 >
编程相关推荐