Skip to content

SyntaxError: invalid syntax with match statement

Quick summary

You used a match/case statement on Python 3.9 or earlier — structural pattern matching requires Python 3.10+.

Why this happens

  • You are running Python 3.9 or earlier where the match/case statement is not supported

  • The match subject or case pattern uses syntax that Python cannot parse in the current version

  • You used 'match' or 'case' as variable names, which became soft keywords in Python 3.10+

Minimal example

✗ Broken code
# Python 3.9 or earlier
match command:
    case 'quit':  # SyntaxError
        exit()
    case _:
        pass
✓ Fixed code
# Rewrite as if/elif for Python < 3.10
if command == 'quit':
    exit()
else:
    pass

# Or upgrade to Python 3.10+ and use match/case

Rewriting match/case as if/elif provides the same logic without requiring Python 3.10+, ensuring backward compatibility.

How to diagnose

  • Check the Python version with python --version — match/case requires Python 3.10+

  • Verify that match and case are used as keywords, not as variable or function names

  • Ensure the case patterns use valid syntax: literals, names, wildcards, or structural patterns

How to fix

  • Upgrade to Python 3.10+ to use match/case, or rewrite as if/elif/else chains

  • Replace match/case with a dictionary dispatch or if/elif chain for compatibility with older Python

  • Rename variables called 'match' or 'case' to avoid shadowing the soft keywords

How to prevent

  • Check the minimum Python version before using match/case in your codebase

  • Use if/elif chains for compatibility with Python 3.9 and earlier, or add a version check

Related Resources

Related Glossary

Related Lessons

← Back to language