TryBugHunt

SyntaxError: invalid syntax

SyntaxError: invalid syntax in Python means python could not parse your code because it violates language rules: usually a missing colon, mismatched brackets, or using = instead of ==.

PythonPattern: Logic errors

How do I fix SyntaxError: invalid syntax?

To fix SyntaxError: invalid syntax in Python, locate the line causing the error and check the values right before it runs. Most occurrences happen because of missing colon at the end of a header. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

What does SyntaxError: invalid syntax mean?

SyntaxError happens at compile time before Python runs a single line of your code. Python's parser reads the token stream and finds something impossible in Python's grammar. Newer Python versions (3.10+) point an arrow directly at the offending character.

Why does SyntaxError: invalid syntax happen?

1. Missing colon at the end of a header

def, if, elif, else, for, while, and try statements must end with a colon.

Breaks

if x > 10
    print("greater")

Works

if x > 10:
    print("greater")

2. Single = inside an if condition

A single = is assignment; comparison requires ==.

Breaks

if user_role = "admin":
    allow_access()

Works

if user_role == "admin":
    allow_access()

3. Unclosed parenthesis, bracket, or string on the line above

The parser keeps waiting for the closing symbol and only flags an error when it hits the next line.

Breaks

total = sum([1, 2, 3
print(total)

Works

total = sum([1, 2, 3])
print(total)

4. Using a reserved keyword as a variable name

class, def, return, for, in, and pass are reserved Python keywords and cannot be assigned to.

Breaks

class = "Mathematics"

Works

class_name = "Mathematics"

How to find it in your own code

Look at the exact line indicated, and if nothing looks wrong, check the line directly above it for an unclosed bracket or quotation mark. Make sure every control flow statement ends in a colon.

Try it: fix this bug

easy

mentions("hello world", "hello") returns False, and mentions("hello world", "xyz") returns True.

def mentions(text, word):
    if text.find(word):
        return True
    return False
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error or pattern. Find the bug and fix it in your browser โ€” no install, no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all logic errors โ†’