Azrion
Azrion

Reputation: 117

How to syntax: returning break and continue statements from a function?

With this question I'm questioning the whole syntax and maybe it's not possible but here we go :)

I'm running a pygame window. Now I have a lot of code in this pygame while loop. Therefore I want to create some functions.

However, the code that I want to create as a function contains continue and break statements. If I want to return break or continue from a static function, obviously it says "U stupid? Continue/Break is outside a loop"

My main question here: How would you design/solve this in a better way?

This is how my code looks like (more or less):

        pygame.init()

        while True:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    pyEvents(event)

             ... # ridiculously long code

             # A small example code snippet that I need as a function
             (grabbed, frame0) = camera.read()
             if not grabbed: # End of feed
                break

             frame1 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
             frame2 = cv2.GaussianBlur(frame1, gaussianBlurKSize, 0)

             if master is None:
                master = frame2
                continue

This is what I want to achieve:

        pygame.init()

        while True:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    pyEvents(event)

             ... # ridiculously long code

             # My dream function
             result = myFunction(camera, master)

             if result == break:
                 break
             elif result == continue:
                 continue

        def myFunction(camera, master):
             (grabbed, frame0) = camera.read()
             if not grabbed:
                return break # Error: outside a loop

             frame1 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
             frame2 = cv2.GaussianBlur(frame1, gaussianBlurKSize, 0)

             if master is None:
                master = frame2
                return continue, master # Error: outside a loop

             return frame2

Upvotes: 1

Views: 83

Answers (3)

pierresegonne
pierresegonne

Reputation: 446

What about something like this?

pygame.init()

while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            pyEvents(event)

        ... # ridiculously long code

        # My dream function
        frame2, master, result = myFunction(camera, master)

        if result == 'break':
            break
        elif result == 'continue':
            continue

def myFunction(camera, master):
    (grabbed, frame0) = camera.read()

    result = None

    if not grabbed:
        return None, None, 'break'

    frame1 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
    frame2 = cv2.GaussianBlur(frame1, gaussianBlurKSize, 0)

    if master is None:
        master = frame2
        return frame2, master, 'continue'

    return frame2, master, result

Upvotes: 1

Amin Guermazi
Amin Guermazi

Reputation: 1732

just make a global boolean variable:

running = True
while running:
    # your code goes here

and if you want to break the loop set running to False:

def myFunction():
    global running
    if you want to break the loop:
        running = False

Upvotes: 3

typewriter
typewriter

Reputation: 368

You could raise an exception in the function. In your loop, you could use a try-except construction. Maybe this link is helpful: https://realpython.com/python-exceptions/

Upvotes: 1

Related Questions