SyntaxError: invalid syntax when using Match case in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# SyntaxError: invalid syntax when using Match case in Python

The "SyntaxError: invalid syntax when using Match case" error is most often caused when:

  1. Using a Python version that is older than 3.10.
  2. Having a syntactical error before the Match-Case statement.
  3. Having a syntactical error in the Match-Case statement.

syntaxerror invalid syntax when using match case

shell
File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 2 match status: ^ SyntaxError: invalid syntax

The syntax for Structural Pattern Matching is the following.

main.py
match subject: case <pattern_1>: <action_1> case <pattern_2>: <action_2> case <pattern_3>: <action_3> case _: <action_wildcard>

Here is an example that prints a string that corresponds to the provided status code.

main.py
def http_error(status): match status: case 200: return "OK" case 400: return "Bad request." case 404: return "Page not found" case 500: return "Internal Server error" case _: return "Something went wrong" print(http_error(400)) # ๐Ÿ‘‰๏ธ Bad request

python match case success

However, if I run the exact same code sample using a Python version older than 3.10, I'd get the error.

python match case error

Notice that I ran the code with Python 3.9, which caused the SyntaxError because Structural Pattern Matching has been added in Python 3.10.

# Checking your version of Python

You can use the python --version command to check your version of Python.

shell
python --version python3 --version

get python version

Alternatively, you can access the version attribute on the sys module.

main.py
import sys print(sys.version)

Run your code with python module_name.py to get your Python version.

If you have an older Python version than 3.10, download the latest version from the official python.org website.

# Implementing your own Match-case statement

If you aren't able to switch to a version of Python that is greater than or equal to Python 3.10, you can implement your own Match-Case statement.

main.py
def http_error(status): status_dict = { 200: 'OK', 400: 'Bad request', 404: 'Not Found', 500: 'Internal Server error', } default_status = 'Something went wrong' return status_dict.get(status, default_status) print(http_error(200)) # ๐Ÿ‘‰๏ธ OK print(http_error(400)) # ๐Ÿ‘‰๏ธ Bad request print(http_error(10000)) # ๐Ÿ‘‰๏ธ Something went wrong
We stored the possible values in a dictionary and used the dict.get() method to return the corresponding value.

If the supplied status is not one of the possible values, we return a default value.

The dict.get() method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

# Having a syntactical error right before the Match-Case statement

Another common cause of the error is having a syntactical error right before the Match-Case statement.

Make sure all the parentheses, curly braces, square brackets, single and double quotes match and have been closed.

You can comment out the Match-Case statement and run your code to verify if it is the cause of the error.

The syntax for Structural Pattern Matching is the following.

main.py
match subject: case <pattern_1>: <action_1> case <pattern_2>: <action_2> case <pattern_3>: <action_3> case _: <action_wildcard>

The match statement takes a value and compares it to the patterns in the case blocks.

If an exact match is not found, the last case, a wildcard _ (if provided) is used as the matching case.
main.py
def http_error(status): match status: case 200: return "OK" case 400: return "Bad request." case 404: return "Page not found" case 500: return "Internal Server error" case _: return "Something went wrong" print(http_error(200)) # ๐Ÿ‘‰๏ธ OK print(http_error(400)) # ๐Ÿ‘‰๏ธ Bad request print(http_error(10000)) # ๐Ÿ‘‰๏ธ Something went wrong

# Conclusion

To solve the "SyntaxError: invalid syntax when using Match case" error, make sure:

  1. You're using a Python version that is greater than or equal to 3.10.
  2. You don't have any syntactical errors before the Match-Case statement.
  3. You don't have any syntactical errors in the Match-Case statement.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev