How to stop a function in a while loop in python

爱⌒轻易说出口 提交于 2020-03-23 09:55:34

问题


Basically I want to stop a function but not by terminating the script.

While True:
  do function A()
  do function B()
  if ( condition ):
    function B.stop()

What I mean is when the condition is met, the while loop still runs but in the next iteration of the while loop, it only does function A, no longer does it do function B. I wonder if we can archive this in python? Thanks


回答1:


How about using a flag within the loop

flag = True

while True:
  if flag:
    do function B()

  if condition:
    flag = false



回答2:


Try this instead

continue_loop = True
While continue_loop:
   do function A()
   do function B()
   if ( condition ):
     continue_loop = False


来源:https://stackoverflow.com/questions/49155280/how-to-stop-a-function-in-a-while-loop-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!