SyntaxError: cannot assign to function call
1 · The lesson
readWhat this error means
A single = stores a value in a name: total = 5 puts 5 into total. The left side must be something that can hold a value: a variable, an item like scores[0], or an attribute like user.name. You put something else on the left: a function call like len(x), a fixed value like 5 or "Ada", or a calculation like x + 1. Python can't store a value into those, so it stops before running anything. Python 3.10 and later also guess the most likely cause and add Maybe you meant '==' instead of '='?.
When you see it
File "main.py", line 2 if len(x) = 5: ^^^^^^ SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
The end of the first part names what was on the left. A number or a string gives literal:
File "main.py", line 1 5 = x ^ SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
A calculation gives expression:
File "main.py", line 2 x + 1 = y ^^^^^ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
Older versions of Python print the message without the Maybe you meant hint.
Why it happens
1. You meant to compare, not to store. = stores a value. == compares two values. Inside an if or while you almost always want ==:
x = [1, 2, 3, 4, 5] if len(x) = 5: print("five items")
2. The two sides are the wrong way round. In maths, 5 = x and x = 5 mean the same. In Python, the name always goes on the left:
5 = x3. You tried to solve an equation. x + 1 = y reads like algebra, but Python does not solve for x. You have to write out the calculation yourself.
4. You tried to change what a function returns. get_user().name = "Ada" is allowed (it sets an attribute on the returned object), but get_user() = other_user is not. A call is a value, not a place to store one.
How to fix it
Use == to compare:
x = [1, 2, 3, 4, 5] if len(x) == 5: print("five items") # five items
Put the name on the left:
y = 10 x = 5 total = x + 1 print(total) # 6
Rewrite an equation so the unknown is alone on the left. For x + 1 = y, that is x = y - 1:
y = 10 x = y - 1 print(x) # 9
Store a result in a new name instead of in the call:
def get_score(): return 70 score = get_score() score = score + 5 print(score) # 75
For a dict or a list, assign to the item, not to a call. d.get("a") = 1 fails, but d["a"] = 1 works:
d = {}
d["a"] = 1
print(d) # {'a': 1}When you'd see this in real code
if user.is_admin() = True:while checking a condition. Use==, or justif user.is_admin():.while input("Again? ") = "y":in a small game loop.- Pandas:
df.loc[0, "price"] = 9.5works, butdf.iloc(0) = row(round brackets) is a call and fails. Use square brackets. - Changing a dict value through
.get(), likecounts.get(word) = 1, instead ofcounts[word] = 1.
Related errors
- SyntaxError: invalid syntax:
if x = 1:with a plain name gives this one instead, with the hintMaybe you meant '==' or ':=' instead of '='?. - SyntaxError: expected ':': the other common typo on
ifandwhilelines. - TypeError: 'str' object does not support item assignment: the left side is allowed, but the object can't be changed.
- UnboundLocalError: an assignment in a function that changes which name Python looks up.
See Also
- All Python errors: the full index, by type and by when it happens.
- Conditionals (tutorial)
- Variables and types (tutorial)
Found this useful? Share it with a friend who’s learning Python.