← Lesson
Match and Case
Joeven
Run
Reset
Python loads on first run
def handle(action): match action: case {"tool": "search", "args": {"q": q}}: return "search " + q case {"tool": "finish", "args": {"text": text}}: return "done: " + text case {"tool": name, "args": args}: return "unknown " + str(name) case _: return "bad action" print(handle({"tool": "search", "args": {"q": "rain"}})) print(handle({"tool": "finish", "args": {"text": "Bring a coat."}})) print(handle({"tool": "shell", "args": {"cmd": "ls"}})) print(handle({"tool": "search"})) print(handle("not a dict")) print(handle({"nope": 1})) print(handle({"tool": "search", "args": {"q": "rain", "k": 3}})) status = 429 match status: case 200: print("status ok") case 401 | 403: print("status forbidden") case 429: print("status slow down") case _: print("status other")
Run to execute this in your browser. Nothing is sent to a server.