[英]Python Breaking While Loop
我正在嘗試跳出 Python 3 中的 while 循環
while not at_end()...:
if ...:
else:
code here
if at_end():
break
然而,這似乎並沒有打破 while 循環。 我也試過將 if 放在 while 循環之后,但它也不起作用。 任何幫助,將不勝感激。
這看起來應該在 for 循環中完成。 但是如果它需要一個while循環,你可以做這樣的事情。
while_flag = True
while while_flag:
if:
something
else:
something else
if at_end():
while_flag = False
你通常會這樣做:
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 循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.