简体   繁体   English

我怎样才能摆脱 while 循环?

[英]How can I get out of while loop?

def solution(ingredient):
    ingredient=np.array(ingredient)
    answer = 0
    while True:
        try:
            for i in range(len(ingredient)-3):
                if (ingredient[0+i:4+i] == [1,2,3,1]).all():
                        answer+=1
                        del_ingredient=np.delete(ingredient,(0+i,1+i,2+i,3+i))

                        if len(del_ingredient)!=len(ingredient):
                            ingredient=del_ingredient
                            break

                        else:
                            raise
        
        except:
            return answer
            break

When I stopped the loop by ctrl c, I obtained the value of answer.当我通过ctrl c停止循环时,我得到了answer的值。 But why I can't get out of the loop???但是为什么我不能跳出循环???

it because you don't have any terminal condition因为你没有任何终止条件

while True simply means run forever so that is the reason why it is running until you hit ctrl+c while True只是意味着永远运行,所以这就是它一直运行到你按下ctrl+c的原因

if you want to stop it at some point the use terminal condition in while statement while (your condition to stop)如果您想在某个时候停止它,请在 while 语句中使用终止条件while (your condition to stop)

thank you谢谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM