0
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. But why I can't get out of the loop???

  • Does this answer your question? [how to break out of only one nested loop](https://stackoverflow.com/questions/18556403/how-to-break-out-of-only-one-nested-loop) – cafce25 Dec 07 '22 at 02:30

1 Answers1

0

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

if you want to stop it at some point the use terminal condition in while statement while (your condition to stop)

thank you

Hariharan
  • 191
  • 7