作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
while
在 for 循环中 i>10 时不会中断:
i = 0
x = 100
while i<=10:
for a in xrange(1, x+1):
print "ok"
i+=1
并打印 "ok"
100 次。当 for
循环中 i
达到 10 时如何中断 while
循环?
最佳答案
直到内层循环“返回”,外层循环中的条件永远不会被重新检查。如果您需要在每次 i
更改后进行此检查,请改为执行此操作:
while i<=10:
for a in xrange(1, x+1):
print "ok"
i+=1
if i > 10:
break
break
只会退出内循环,但由于外循环条件的计算结果为 False
,它也会退出。
关于python - 如何在 python 的内部 for 循环中中断 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13962424/
我是一名优秀的程序员,十分优秀!