Python SyntaxError: can’t assign to function call Solution

Posted in /  

Python SyntaxError: can’t assign to function call Solution
vinaykhatri

Vinay Khatri
Last updated on March 5, 2024

    To assign a value to a variable in Python, we use the assignment operator between the variable name and value. And during this process, the variable name must be on the left side of the assignment operator and the value on the right side.

    We can also write functions in Python that return values when they are called. But if we try to assign a value to a function call statement, we will receive the SyntaxError: can’t assign to function call error in Python.

    In this Python guide, we will discuss why this error occurs in Python and how to solve it. We will also discuss an example that demonstrates this error in Python to get a better idea of this Python error. So let's get started with the Error statement.

    Python Error: SyntaxError: can’t assign to function call

    The PythonError Statement SyntaxError: can’t assign to function call here. Maybe you meant '==' instead of '='? is divided into two parts separated by colon : .

    1. Error Type ( SyntaxError ): SyntaxError Occurs in Python when we write some wrong syntax in the program.
    2. Error Message ( can’t assign to function call This is the actual error message telling us that we are trying to assign a value to a function call statement in Python, which is invalid.

    Error Reason

    In Python, we can use the assignment operator = to assign a value to a variable name, but if we try to assign a value to a function call statement, then we receive the SyntaxError: can’t assign to function call Error.

    Example

    total = 0
    
    bill = [20, 30, 40, 50]
    
    # assign value to sum() function (error)
    sum(bill) = total
    
    print(total)

    Output

    File "main.py", line 6
    sum(bill) = total
    ^
    SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?

    Break the Code

    In the above example, we are getting the error at line 6 with sum(bill) = total the statement. The reason for the error is quite obvious. In line 6, we are trying to assign the value of total to the function call statement sum(bill) , which is invalid syntax in Python.

    Solution

    The solution for the above example is very simple. All we need to do is put the total variable on the left side of the assignment operator = , and sum(bill) function call on the right side. So the value of the sum(bill) statement can be assigned to the total variable.

    total =0
    
    bill = [20, 30, 40, 50]
    
    # assign value to total
    total =sum(bill)
    
    print(total)

    Output

    140

    Common Scenario

    The most common scenario where many new Python learners commit this mistake is when they misplace the variable and function call statement during the assignment operation. The variable to which we want to assign the value must be on the left side of the assignment operator = , and the function that returns the value must be on the right side of the assignment operator.

    Let's take an example where and write the function call on the left and variable name on the right side.

    Error Example

    def percentage(marks):
        marks_obtain = sum(marks)
        maximum_marks = len(marks) *100
    
        return round((marks_obtain/maximum_marks)*100, 2)
    
    my_marks = [90, 90, 90, 90, 90]
    
    # assing value to function call 
    percentage(my_marks) = total_percentage
    print(total_percentage)

    Output

     File "main.py", line 9
    percentage(my_marks) = total_percentage
    ^
    SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?

    Break The code

    If we read the error statement, we can tell that at line 9, we are getting this error because we are trying to assign the total_percentage variable to the percentage(my_marks) function call statement. And according to the Python syntax, it is illegal.

    Solution

    To solve this problem, we need to swap the position of the percentage(my_marks) function call and total_percentage variable. So the returned value of percentage() could be assigned to the total_percentage variable.

    def percentage(marks):
        marks_obtain = sum(marks)
        maximum_marks = len(marks) *100
    
        return round((marks_obtain/maximum_marks)*100, 2)
    
    my_marks = [86, 99, 95, 80, 70]
    
    # assing value to function call
    total_percentage = percentage(my_marks)
    
    print(total_percentage, '%')

    Output

    86.00 %

    Final Thoughts!

    The SyntaxError: can’t assign to function call error occurs in Python when we try to assign a value to a function call statement. This exception is a result of when we write the variable on the right side and function call on the left side of the assignment operation.

    To debug this error, we must make sure that whenever we try to assign a return value of a function call to a variable, the function must be on the right side and the variable on the left side.

    If you are still getting this error in your Python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments