оператор 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