Syntaxerror can’t assign to function call here

There are times when you try to call the function the syntaxerror can’t assign to function call here raised in your program.

It means you’ve done something wrong in your code.

So today, we are going to deal with syntaxerror: cannot assign to function here error message.

If you are stuck to this error, then this article is indeed for you.

This article discusses important information that you need to know in order to fix this error.

What is “syntaxerror: cannot assign to function call here” error message?

The syntaxerror can’t assign to function call here occurs when you are trying to assign a value to a function call that cannot be granted by the Python interpreter.

This error message indicates that you are trying to assign a value to a function rather than trying to assign a function call to a variable.

In a nutshell, syntaxerror: cannot assign to function call here raised when you placed a function call on the left-hand side of the equal sign of a variable.

For example:

def add(x, y):
    return x + y

add(10, 20) = 30

Output:

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

If you are confused and asking if syntaxerror: cannot assign to function call here and syntaxerror can’t assign to function call are the same.

Yes, they are the same, so you don’t have to worry. The solutions are the same as well.

Why does the “can’t assign to function call here” SyntaxError occurs in Python?

The can’t assign to function call here SyntaxError occurs in Python when you are trying to attempt assigning a value to a function call.

This error arises because, in most programming languages, including Python, function calls are meant to invoke or execute the function, not to be assigned values directly.

In Python, when you write:

 mysample_function() = 30

It violates the rules of language’s syntax because you’re trying to assign the value 30 to the function called mysample_function().

Python interprets this as a SyntaxError because it expects an identifier or a variable on the left side of the assignment operator (=) and not a function call.

How to fix “syntaxerror can’t assign to function call here”?

To fix the syntaxerror: cannot assign to function call here error in Python, ensure that you are not trying to assign a value to a function call. Instead, you should assign the value returned by the function call to a variable.

Here are the following solutions that you can use as your basis to resolve the error you are currently facing.

Solution 1: Specify the function call

You have to specify first the variable name on the left side, followed by an equals sign, and then specify the function call on the right side.

Incorrect code

def add(x, y):
    return x + y

add(10, 20) = 30

Rather than writing add(10, 20) = 30, you should write result = add(10, 20), where the result is a variable that can store the value returned by the function call add(10, 20).

Corrected code

def add(x, y):
    return x + y

result = add(10, 20)
print(result)

In the corrected code, we assign the value returned by the function call add(10, 20) to the variable result instead of trying to assign it directly to the function call.

Output:

30

Solution 2: Use double equals

If you want to perform equality comparison, you can use double equals.

For example:

a = 50
b = 100

if a == b:
    print("a and b are equal")
else:
    print("a and b are not equal")

Output:

a and b are not equal

Another example:

website = 'itsourcecode'

if website == 'itsourcecode':
    print('success')
else:
    print('failure')

Output:

success

Solution 3: Use square brackets

If you encounter this error while working with the dictionary, it is because we use parentheses rather than using brackets [] when adding a new key-value to the dict.

For example:

sample_dict = {}

sample_dict('website') = 'Itsourcecode.com'

Note: Ensure to use square brackets “[]” when you need to access a key-value pair in a dictionary.

Corrected code:

sample_dict = {}

sample_dict['website'] = 'Itsourcecode.com'

print(sample_dict['website'])

Output:

Itsourcecode.com

Solution 4: Use the correct syntax

If you encounter this error when dealing with list comprehensions, it is because you are using it incorrectly. You should use the correct syntax to avoid this error.

For example:

def website():
    return 'itsourcecode'


result = [char for website() in char]

print(result)

As you can see in this example code, the order in the list comprehension is not correct. So as a result, it will throw an error message.

Output:

 File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 5
    result = [char for website() in char]
                       ^^^^^^^^^
SyntaxError: cannot assign to function call

Rather than using result = [char for website() in char], it should result = [char for char in website()].

Corrected code:

def website():
    return 'Itsourcecode'

result = [char for char in website()]

print(result)

Output:

['I', 't', 's', 'o', 'u', 'r', 'c', 'e', 'c', 'o', 'd', 'e']

Additional solutions for “syntaxerror: cannot assign to function call here” 

If the above solutions do not resolve the error, maybe because you miss something. You can check the following solutions that can help you fix the error.

Double-check your code

Carefully examine the line of code where the error occurs and identify the specific function call that caused the problem.

Check variable assignment

Verify that you are attempting to assign the return value of the function call to a variable. If not, you may need to rethink your code logic.

Separate the assignment and function call

Rather than assigning value to the function call directly, you can use a temporary variable to store the result of the function call first. And then assign that variable to the desired target.

Conclusion

In conclusion, the error message syntaxerror can’t assign to function call here occurs when you are trying to assign a value to a function call that cannot be granted by the Python interpreter.

This error message indicates that you are trying to assign a value to a function rather than trying to assign a function call to a variable.

To fix this SyntaxError error in Python, ensure that you are not trying to assign a value to a function call. Instead, you should assign the value returned by the function call to a variable.

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 😊

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