79205641

Date: 2024-11-20 02:42:51
Score: 1
Natty:
Report link

This is a somewhat elegant solution that works if you are running the code in a function:

def foo(x):
    c = 1
    match x:
        case "Hello,":
            a()
        case "World!":
            b()
        case "foobar":
            c()
        case _:
            print("Something didn't happen :(")
            c = 0
    if c == 1:
        print("Something happened")

This method makes use of the builtin _ case to run code when NONE of the previous cases are met, and sets the variable once.

If you are running this at the end of the function, you can return and skip the need for the variable:

...
        case _:
            print("Something didn't happen :(")
            return
    print("Something happened")
Reasons:
  • Blacklisted phrase (1): :(
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Klumpy7