我正在尝试打破Python3中的while循环
while not at_end()...:
if ...:
else:
code here
if at_end():
break但是,这似乎不会中断while循环。我还尝试将if放在while循环之后,但它也不起作用。任何帮助都将不胜感激。
发布于 2019-12-04 02:05:00
这看起来应该在for循环中完成。但是如果它需要一个while循环,你可以这样做。
while_flag = True
while while_flag:
if:
something
else:
something else
if at_end():
while_flag = False发布于 2019-12-04 02:05:56
您通常会这样做:
not_at_end = True
i = 0
while not_at_end:
if i < 3:
print('do stuff')
i += 1
else:
print('do other stuff')
not_at_end = False
# do stuff
# do stuff
# do stuff
# do other stuff迭代器(i)只是为了显示示例代码。要点是使用一个布尔值来中断while循环。
https://stackoverflow.com/questions/59162897
复制相似问题