match statements
Menus are everywhere: dial a USSD code and press 1 for balance, 2 for airtime, 3 for
help. You can already build one with if and elif. In this lesson you'll learn
match, a statement made for exactly this job, and one trap in it that looks
harmless but catches everything.
From elif to match
Here's a menu written the way you know:
choice = "2"
if choice == "1":
print("Check balance")
elif choice == "2":
print("Buy airtime")
elif choice == "3":
print("Help")
else:
print("Unknown choice")In the editor, press Escape then Tab to move on.
Every branch compares the same name with a fixed value. match says that once:
choice = "2"
match choice:
case "1":
print("Check balance")
case "2":
print("Buy airtime")
case "3":
print("Help")
case _:
print("Unknown choice")In the editor, press Escape then Tab to move on.
match choice: names the value to look at. Each case gives a value it might be,
called a
.
Python tries the cases from the top and runs the first one that matches, just like an
elif chain. case _: matches anything, so it catches every other choice, like
else. Put it last. Change choice to "7", then to 2 without quotes: the number
2 isn't the text "2", so it falls through to _.
Several values in one case
| inside a case means "or": the case matches any of the values. Predict this.
What will this print?
Decide before you look. Guessing wrong is how this sticks.
answer = "N"
match answer:
case "yes" | "y" | "Y":
print("Let's go!")
case "no" | "n" | "N":
print("Maybe later")
case _:
print("Please answer yes or no")Python prints
Maybe later
"N" is one of the values in the second case, so that case runs. Unlike or in an if, | in a case lists values to compare with: no repeating the name needed.
Remember the or bug from lesson 2.3? | in a case is the version that reads the way
you'd hope: each value is compared with answer.
A coffee ceremony has three rounds, abol, tona, and bereka. Given the round number, name the round:
round_number = 2
match round_number:
case 1:
print("Abol")
case 2:
print("Tona")
case 3:
print("Bereka")
case _:
print("The ceremony has three rounds")In the editor, press Escape then Tab to move on.
The name that catches everything
A pattern must be a value, like "red" in quotes. A bare name without quotes means
something else entirely:
_ is the one bare name you should use: it's the "anything else" case.
When to use match, and when not
match shines when a value is one of a few fixed options: menu choices, commands,
answers, round numbers. It isn't for ranges. A grade depends on whether a score is
at least 85 or at least 70, and a case can't ask "at least". For ranges, use if
and elif, as in lesson 2.2.
Quick check
Which job suits match best?
Exercise
A command menu
Ask for a command and answer it with match. The balance is 150 birr. For
balance, print it; for buy, print that airtime was bought; for help, list the
commands; for anything else, say the command is unknown.
Command: balance
Your balance is 150 birrYour code
balance = 150
command = input("Command: ")
# Answer balance, buy, help, and anything elseIn the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
Show a solutionHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
balance = 150
command = input("Command: ")
match command:
case "balance":
print(f"Your balance is {balance} birr")
case "buy":
print("Airtime bought")
case "help":
print("Commands: balance, buy, help")
case _:
print(f"Unknown command: {command}")Key takeaways
matchcompares one value with several patterns and runs the firstcasethat matches."yes" | "y"in a case matches either value;case _:matches anything else and goes last.- A bare name like
case red:catches everything; put quotes around text patterns. - Use
matchfor a few fixed options, andif/eliffor ranges.