← Back to Blog

Common MATCH/CASE Mistakes

The problem...

Your match block runs the wrong case. Or no case at all. Or it crashes on Python 3.9.

These are the mistakes almost every beginner makes with match / case. At least once.

Mistake 1 — Using match on Python 3.9 or earlier

# Wrong — SyntaxError on Python 3.9 or earlier
match command:
    case "quit":
        print("Quitting.")
# Check your version first
import sys
print(sys.version)

match / case requires Python 3.10 or later. If you see a SyntaxError — check your version first.

Mistake 2 — Forgetting case _

# Wrong — no fallback
match command:
    case "quit":
        print("Quitting.")
    case "help":
        print("Help.")
# user types "start" — nothing happens, no error
# Right — always handle the unexpected
match command:
    case "quit":
        print("Quitting.")
    case "help":
        print("Help.")
    case _:
        print("Unknown command.")

Without case _, unmatched values are silently ignored. Always add a wildcard.

Mistake 3 — Case sensitive matching

command = input("Enter command: ")    # user types "Quit"

match command:
    case "quit":                       # doesn't match "Quit"
        print("Quitting.")
    case _:
        print("Unknown command.")      # runs instead
# Right — normalize before matching
match command.lower():
    case "quit":
        print("Quitting.")

match is case-sensitive. Normalize with .lower() before matching strings.

Mistake 4 — Putting case _ in the wrong place

# Wrong — case _ blocks everything after it
match command:
    case _:
        print("Unknown.")
    case "quit":               # never reached
        print("Quitting.")
# Right — case _ always goes last
match command:
    case "quit":
        print("Quitting.")
    case _:
        print("Unknown.")

case _ matches everything — put it first and nothing else runs. Always last.

Mistake 5 — Using match when if / elif is clearer

# Awkward — match forced on unrelated conditions
match score:
    case n if n > 90 and attendance > 80:
        print("Honours.")

# Better — if / elif is clearer here
if score > 90 and attendance > 80:
    print("Honours.")

match shines when checking one variable against many values. For complex, multi-variable logic — if / elif is still the right tool.

What's really happening

Most match mistakes are about assumptions — assuming it works on older Python, assuming it's case-insensitive, assuming case _ can go anywhere.

It's a clean tool. Use it cleanly.

The mindset shift

Stop thinking: "match is just a fancier if."

Start thinking: "match has its own rules — know them."

What you should understand now

  • match requires Python 3.10+ — check your version
  • Always add case _ — unmatched values are silently ignored
  • Normalize strings with .lower() before matching
  • case _ always goes last — it matches everything
  • Use match for one variable, many values — not for complex logic
[ login to bookmark ] // copied! 23 views · 2 min
// resources
Other match_case_mistakes.py
← prev MATCH Mini Project — Vending Machine. next → match / case — The Full Picture
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.