match

Choose a branch from one value. case _ is the default and must come last.

Syntax

match command:
case "N":
...
case _:
...
  • case "N" | "S": shares one body. Quotes are required for text.

Example

cmd = "SCAN"
match cmd:
    case "N" | "S":
        print("move")
    case "SCAN":
        print("look")
    case _:
        print("wait")

Output:

look

Common mistakes

  • case _ matches everything, so it must be last. "n" and "N" are different values.

Practise it