You cannot assign the result of a calculation to a mathematical operator. If you perform a calculation before an assignment operator appears on a line of code, you’ll encounter the SyntaxError: cannot assign to operator
error.
In this guide, we discuss what this error means and why you may run into it in your programs. We’ll walk through an example of this error so you can learn how to solve it in your code.
SyntaxError: cannot assign to operator
The assignment operator (=) lets you set a value for a variable. Calculations can only appear on the right-hand side of the operator. Consider this line of code:
a = 1 * 3
Our program sets the result of evaluating 1 * 3 to the variable “a”. We can now access the result of our calculation at any time in our program by referring to the value “a”.
The name of the variable to which we want to assign a value comes first. Then, we specify an equals sign. This equals sign denotes we want to assign a value to a variable. Following the equals sign, we specify a calculation.
If you want to assign the result of a sum to a variable, you must use this format. You should not try to evaluate a problem on the left side of an assignment operator.
An Example Scenario
We’re going to write a program that calculates the profits a cafe has made on the coffee orders placed in the last day. To start, let’s define a list. This list will contain all of the purchases customers have made on their range of coffees:
orders = [2.20, 2.40, 2.60, 2.40, 2.80]
We’re going to declare and assign a placeholder value to a variable called “profits” which tracks the cumulative profits made on all of the drinks:
profits = 0
For now, “profits” is equal to 0. To calculate the value of profits, we are going to subtract $2.00 from the cost of every coffee. This is approximately how much it costs to produce any coffee at the cafe. To do this, we’re going to use a for loop:
for o in orders: o - 2.00 += profits
This code iterates through all of the coffees in our list. We subtract $2.00 from the value of each coffee. We then use the addition assignment operator (+=) to add the value we calculate to the variable “profits”.
Finally, let’s print out a message to the console that informs us how much the cafe has earned in profit over the last day from their coffee sales:
print("The cafe has earned ${} in profit from coffee sales today.".format(profits))
Let’s run our code and see what happens:
File "main.py", line 6 o - 2.00 += profits ^ SyntaxError: cannot assign to operator
Our code returns an error.
The Solution
We’ve tried to evaluate a sum before an assignment operator.
This is invalid syntax. We must specify a variable name, and then an assignment operator, followed by the sum we want to evaluate.
Python is programmed to interpret the statement before an assignment operator as a variable name. Written in English, Python tries to read our code as:
"Subtract 2.00 from the value "o". Consider this a variable name. Add the value of "profits" to the variable."
This doesn’t make sense. We want to subtract 2.00 from the value “o” and then add the result of our sum to the variable “profits”.
To fix this error, we need to swap around the “profits” and sum statements in our code:
profits += o - 2.00
Python will read this code as “Evaluate o – 2.00 and add the result of that sum to the “profits’ variable”. This is what we want our program to accomplish.
Let’s run our code:
The cafe has earned $2.4 in profit from coffee sales today.
Our program successfully calculates how much the cafe has earned in profit. The cafe, after subtracting 2.00 from the value of each order, has earned a profit of $2.40 in the last day from coffee sales.
Conclusion
The SyntaxError: cannot assign to operator
error is raised when you try to evaluate a mathematical statement before an assignment operator.
To fix this error, make sure all your variable names appear on the left hand side of an assignment operator and all your mathematical statements appear on the right.
Now you have the knowledge you need to fix this error like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
Hi!
I was wondering if you could show an example? That’d be great, thanks!
Kind regards
Jules