[英]How to exit outer loop from an inner if statement
我在下面的表格中有一个循环。 如果p>len(number)
我想打破并退出整个while循环。 Number是包含一些数字的列表。 当p大于len(数字)时,下面的代码不会从整个循环中断。 有人可以帮助python中的实现。
while number[p]<0 :
if "some condition":
#do something
p=p+1
if p>len(number):
break;
else:
#do something
p=p+1
if p>len(number)
break;
请参考以下两个链接:
希望它会有所帮助..
如果您希望在p
大于len(number)
不运行代码,请将其添加到while
循环的条件中:
while p <= len(number) and number[p] < 0:
if "some condition":
#do something
else:
#do something
p=p+1
没有必要在if
/ else
两个部分重复使用相同的代码 - 只需将其移出,直接在while
。
这应该是for循环:
for value in number:
if condition:
do_something()
else:
do_other()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.