Syntaxerror: cannot assign to function call

Are you dealing with the Python syntaxerror: cannot assign to function call error message? 

And are you confused about how you are going to resolve it? Worry no more! Instead, keep on reading.

It is because this article discusses the solutions wherein it can help you to fix the syntaxerror cannot assign to function call.

So let’s get started and enhance your programming expertise!

What is “syntaxerror cannot assign to function call”?

The error message syntaxerror: can’t assign to function call occurs when you are trying to assign a function call to a variable in reverse order.

For example:

def sample_num():
    return 500

sample_num() = 'xyz'

Output:

 File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 4
    sample_num() = 'xyz'
    ^^^^^^^^^^^^
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?

This error indicates that you have a function call on the left side of an assignment statement, which is not allowed in Python. You can only assign values to variables, not to function calls.

Note: If you want to assign the result of a function in a variable, you should specify the variable name, followed by an equals sign, and followed by the function you wanted to call.

Why does the “Syntaxerror: cannot assign to function call”error occur?

This error can occur because of some reasons, such as:

👎 This error often occurs when the assignment operator (=) is mistakenly used instead of the equality operator (== or ===) within an if statement or conditional expression.

👎 Trying to assign a value to a function directly can result in this error.

👎 Careless mistakes, such as misspelling a variable or function name, can lead to unintended function calls and trigger the mentioned error.

👎 Assigning a value of an incompatible data type to a function call can result in this error. Ensure that the assigned value matches the expected data type of the function.

👎 Incorrect usage of function calls, such as missing parentheses or using incorrect arguments, can lead to the SyntaxError.

How to fix “Syntaxerror: cannot assign to function call”?

To fix the syntaxerror cannot assign to function call error in Python, ensure that you are not trying to assign a value to a function call.

Instead of having a function call on the left side of an assignment statement, you should have a variable.

Solution 1: Define the function call

This is the solution for the example code that we illustrate above. So in order to fix the error, ensure to define the function call on the right-hand side of the assignment.

For example:

def sample_num():
    return 500

mysample_num = sample_num()

print(mysample_num)

Output:

500

Solution 2: Use double equals

For example:

def sample_num():
    return 100

if 500 == sample_num():
    print('True')
else:
    print('Falls')

Note: We only use double equals “==” if you want the comparison and single equals “=” for the assignment.

Output:

Falls

Solution 3: Use brackets

For example:

sample_dict = {}

sample_dict['website'] = 'Itsourcecode'

print(sample_dict['website'])

Output:

Itsourcecode

Conclusion

In conclusion, the error message syntaxerror: can’t assign to function call occurs when you are trying to assign a function call to a variable in reverse order.

This article already discussed what this error is all about and multiple ways to resolve this error.

By executing the solutions above, you can master this Syntaxerror with the help of this guide.

You could also check out other “SyntaxError” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Why assignment SyntaxError happens

Assignment SyntaxErrors usually come from confusing = (assignment) with == (comparison), or trying to assign to something that isn’t a valid target.

Common triggers

  • Comparison instead of assignment in if. if x = 5: — should be ==.
  • Assign to a literal. 5 = x or "str" = x — literals cannot be assignment targets.
  • Assign to function call. func() = x — cannot assign to a return value.
  • Assign to expression. a + b = c — expressions are not targets.
  • Unbalanced multi-assign. a, b = 1 — right side must match target count.

Diagnostic pattern

# BAD — = instead of ==
if x = 5:  # SyntaxError
    ...

# GOOD — use ==
if x == 5:
    ...

# Or use walrus for assignment + test
if (x := compute()) == 5:
    ...

# BAD — assign to function call
sorted(items) = new_list  # SyntaxError

# GOOD — assign the sorted result
new_list = sorted(items)

# BAD — mismatched targets
a, b = 1  # ValueError at runtime, but static tools warn

# GOOD
a, b = 1, 2

Best practices

  • Use ruff or Pyright. Catches most assignment mistakes statically.
  • Prefer walrus for assign-in-condition. Clearer than nested statements.
  • Use type hints. Editors flag shape mismatches during autocomplete.

Frequently Asked Questions

What is Python SyntaxError and what causes it?

SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.

How do I fix ‘unexpected EOF while parsing’?

Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.

Why does my syntax error point to a line that looks correct?

The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.

Why does ‘print x’ raise SyntaxError in Python 3?

Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.

Where can I find more SyntaxError fixes?

Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment