Shackman
Shackman

Reputation: 61

Is there a way to detect if a loop has stopped in python

I am trying to detect when my loop has stopped so that i could use an if statement saying: if "the loop has stopped": print(text)

Code:

faces = detector(gray)

for face in faces:

    x, y = face.left(), face.top()
    x1, y1 = face.right(), face.bottom()
    rectangle = cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)

    if "the loop has stopped":
        cv2.putText(frame, "Asleep", (50, 150), font, 7, (0, 0, 255))

Upvotes: 3

Views: 2208

Answers (4)

Asnim P Ansari
Asnim P Ansari

Reputation: 2477

You can use for ... else construct for your specific use case. It looks like

for loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement. They are really useful once you understand where to use them. I, myself, came to know about them a lot later.

The common construct is to run a loop and search for an item. If the item is found, we break out of the loop using the break statement. There are two scenarios in which the loop may end. The first one is when the item is found and break is encountered. The second scenario is that the loop ends without encountering a break statement. Now we may want to know which one of these is the reason for a loop’s completion. One method is to set a flag and then check it once the loop ends. Another is to use the else clause.

This is the basic structure of a for/else loop:

for item in container:
    if search_something(item):
        # Found it!
        process(item)
        break
else:
    # Didn't find anything..

Upvotes: 6

Chris Johnson
Chris Johnson

Reputation: 21956

This can be done; but it’s not clear why you would want to.

If there was a strong reason to detect the state of the loop while in the loop, you could avoid using a for loop and iterate manually by converting the iterable (faces) to an iterator via the iter function. You’ll know when the iterator is concluded — when you call next, you’ll get a StopIteration exception.

A for loop is just syntactic sugar for that operation — a lot easier to work with.

I’m curious why you want to do this instead of just letting the for loop conclude and doing what you need on the line after the loop.

Upvotes: 0

Tim
Tim

Reputation: 2049

As other people have said in comments, once the for loop has been through every face in faces it will carry on executing code. You don't need to do anything special to make it stop looping, unless you have a reason to break out of that process earlier (before it has been through every face).

So you just need to un-indent the statement at the end.

I think you probably want this:

faces = detector(gray)

for face in faces:
    x, y = face.left(), face.top()
    x1, y1 = face.right(), face.bottom()
    rectangle = cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)

# this will run after the for loop has finished
cv2.putText(frame, "Asleep", (50, 150), font, 7, (0, 0, 255))

Upvotes: 2

Akash Deodhar
Akash Deodhar

Reputation: 70

I would really appreciate it if you can post the full code/ at least the loop you are referring to in the question details.

I don't think there is a way to detect if a loop has ended or not in python. If you want to check if a for loop went from range i to n completely without any problem then you iterator value can be checked with the (max-1) of your range function. if the for loop has break statement in between somewhere or any other issues causing the loop to terminate abruptly the iterators value would be lesser than (max-1) of your range function

Upvotes: 0

Related Questions