这是用于打印列表中存在的最低正整数的代码。 break语句不起作用,循环无限运行。
list = []
n=1
print("enter array")
for i in range (5) :
a=(int(input()))
list.append(a)
while n<4 :
for i in range (5) :
if(list[i]== n):
n=n+1
continue
else:
print("the number should be" , n)
break
答案 0 :(得分:1)
break语句是指最内部的循环级别
下面的代码是一个无限循环:
while True:
for i in range(10):
if i == 5:
break # breaks the for, start a new iteration of the while loop
要打破while循环,您可以考虑使用这种标志
while True:
broken = False
for i in xrange(10):
if i == 5:
broken = True
# break the for loop
break
if broken:
# break the while loop
break
for-else语句在这里也可能会有所帮助:
while True:
for ...:
if ...:
# break the for loop
break # refers to the for statement
else:
# the breaking condition was never met during the for loop
continue # refers to the while statement
# this part only execute if the for loop was broken
break # refers to the while statement