5

I'm trying to use match case when checking the value in event loop. However break not only break the match case, but break the event loop too.

This is the code

while True:
    # Some code stuff here
    if event == "#PassSign":
        # Some code stuff again to check password strength
        # Display the password strength
        match strength_pass:
            case 0:
                window["#StatusPassSign"].update("No Password", visible=True)
                break
            case 1:
                window["#StatusPassSign"].update("Password Strength: Low", visible=True)
                break
            case 2:
                window["#StatusPassSign"].update("Password Strength: Medium", visible=True)
                break
            case 3:
                window["#StatusPassSign"].update("Password Strength: High", visible=True)
                break

How to break/stop the match case, without stop the event loop?

Marius ROBERT
  • 426
  • 4
  • 15
Franky Ray
  • 83
  • 1
  • 6

1 Answers1

5

There is no fall through in Python, so no need for trailing break.

What if you need an early break? You can emulate that with exceptions:

class _MyBreak(Exception): pass

foo = 5
bar = 3
try:
    match foo:
        case 5:
            if bar > 1:
                print("about to break")
                raise _MyBreak()
            print("not reached")
        case 42:
            pass
except _MyBreak:
    pass
0xF
  • 3,214
  • 1
  • 25
  • 29
  • 2
    This is correct. Remember in Python, `break` statements /can only belong to a `for` or `while` loop/. Nothing else. – BadZen Mar 27 '23 at 13:50