Variables cannot be assigned to literal values like strings or numbers. If you try to assign a variable to a literal, you’ll encounter the “SyntaxError: can’t assign to literal” error.
This guide discusses what this error means and why you may encounter it. We’ll explore an example of how to fix this error so that you can build the knowledge you need to solve this error in your own code.
SyntaxError: can’t assign to literal
A variable is a unique identifier for a value. When you reference a variable, Python will read the value associated with that variable. Python uses an equals sign to assign values to a variable:
language = "Python"
“language” is the name of our variable. The equals sign tells Python we are assigning a value to the variable. “Python” is the value that is associated with our variable.
Variables are containers for values. They are not values in themselves. This means you cannot give a variable a name that is equal to a value. If variable names could be equal to a value, Python would not be able to distinguish variables from values.
Variables cannot be strings, booleans, or any other data type. They must be declared using the syntax that we discussed above.
An Example Scenario
We’re going to build a program that calculates a discount for a product in a donut store. To start, ask the user to insert a price of a donut:
"price" = float(input("Enter the price of a donut: "))
Next, ask the user to choose the percentage discount that should be applied to the donut:
"discount" = float(input("Enter the discount percentage: "))
The input()
method returns a string. We cannot run mathematical operations on a string so we need to convert the data we gather to a numerical value. We’ve converted both of these values to floats using the float() method.
Now that we have these two values, we can calculate the discounted price:
discounted_price = round(price - ((price / 100) * discount), 2) print("The new cost of a donut is ${}.".format(discounted_price))
We calculate the discounted price using a percentage decrease formula. We round the discounted price to the nearest two decimal places. Then, we print out the discounted price to the console.
Let’s run the code:
File "main.py", line 1 "price" = input("Enter the price of a donut: ") ^ SyntaxError: cannot assign to literal
We have run into an error.
The Solution
“price” is a literal value. It is a string. We have tried to assign a value to this literal. This causes an error because variable names must be identifiers, not values.
We can fix this error by removing the quotation marks around our first two variables:
price = float(input("Enter the price of a donut: ")) discount = float(input("Enter the discount percentage: "))
Our variables now have unique identifiers. We can use the labels price and discount to identify these values. Let’s run our program:
Enter the price of a donut: 2.20 Enter the discount percentage: 5 The new cost of a donut is $2.09.
Our code successfully calculates the discounted value of a product.
Conclusion
The “SyntaxError: can’t assign to literal” error occurs when you try to assign a value to a literal value such as a boolean, a string, a list, or a number. To solve this error, ensure your variable names are names rather than values.
Now you have the knowledge you need to fix this error like a professional!
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.