PythonMastery
reference 3 min read · lesson 72 of 80 in Errors

SyntaxError: cannot assign to function call

1 · The lesson

read

What 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

python
  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:

python
  File "main.py", line 1
    5 = x
    ^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

A calculation gives expression:

python
  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 ==:

python
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:

python
5 = x

3. 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:

python
x = [1, 2, 3, 4, 5]
if len(x) == 5:
    print("five items")     # five items

Put the name on the left:

python
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:

python
y = 10
x = y - 1
print(x)                    # 9

Store a result in a new name instead of in the call:

python
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:

python
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 just if user.is_admin():.
  • while input("Again? ") = "y": in a small game loop.
  • Pandas: df.loc[0, "price"] = 9.5 works, but df.iloc(0) = row (round brackets) is a call and fails. Use square brackets.
  • Changing a dict value through .get(), like counts.get(word) = 1, instead of counts[word] = 1.

See Also

Found this useful? Share it with a friend who’s learning Python.