Last updated: Apr 10, 2024
Reading timeยท3 min
The "SyntaxError: invalid syntax when using Match case" error is most often caused when:
File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 2 match status: ^ SyntaxError: invalid syntax
The syntax for Structural Pattern Matching is the following.
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.
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
However, if I run the exact same code sample using a Python version older than 3.10, I'd get the 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.
You can use the python --version
command to check your version of Python.
python --version python3 --version
Alternatively, you can access the version
attribute on the sys
module.
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.
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.
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
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:
Name | Description |
---|---|
key | The key for which to return the value |
default | The 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
.
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.
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 provided) is used as the matching case.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
To solve the "SyntaxError: invalid syntax when using Match case" error, make sure: