SyntaxError: '(' was never closed
What it means
You opened a bracket, (, [ or {, and Python reached the end of the file without finding its closing partner. The caret points at the opening bracket, so the missing one belongs somewhere after it.
1. A missing `)` at the end of a function call
This raises the error
Broken
scores = [72, 88, 95]
print("Average:", sum(scores) / len(scores)
Output
Traceback (most recent call last):
File "<exec>", line 2
print("Average:", sum(scores) / len(scores)
^
SyntaxError: '(' was never closedThe fix
Fixed
scores = [72, 88, 95]
print("Average:", sum(scores) / len(scores))
Output
Average: 85.0
Nested calls need one ) for every (. Here print(, sum( and len( each need their own, so the line ends with two.
2. A list that is never closed
This raises the error
Broken
colors = ["red", "green", "blue"
print(colors)
Output
Traceback (most recent call last):
File "<exec>", line 1
colors = ["red", "green", "blue"
^
SyntaxError: '[' was never closedThe fix
Fixed
colors = ["red", "green", "blue"]
print(colors)
Output
['red', 'green', 'blue']
Python lets a list continue onto the next lines, so it kept reading through the rest of the file and never found the ].
3. A dictionary over several lines, missing its `}`
This raises the error
Broken
ages = {
"Ada": 36,
"Alan": 41,
print(ages["Ada"])
Output
Traceback (most recent call last):
File "<exec>", line 1
ages = {
^
SyntaxError: '{' was never closedThe fix
Fixed
ages = {
"Ada": 36,
"Alan": 41,
}
print(ages["Ada"])
Output
36
When a dictionary or list spans several lines, put the closing bracket on its own line, lined up with the start. That makes a missing one easy to spot.
When you see this error, check
- Go to the bracket Python points at and decide where its partner should be.
- Count the opening and closing brackets on that line: the numbers should match.
- Check the kind as well as the number:
(closes with),[with],{with}. - An error reported on a later line can come from a bracket left open on an earlier one.
Related errors
All Python errors · How to read a traceback · Step through your code