My task is to write a program that asks the user to enter 5 names which it stores in a list. Next, it picks one of these names at random and declares that person as the winner. The only issue is that when I try to run it, it says can't assign to literal
.
This is my code:
import random
1=input("Please enter name 1:")
2=int(input('Please enter name 2:'))
3=int(input('Please enter name 3:'))
4=int(input('Please enter name 4:'))
5=int(input('Please enter name 5:'))
name=random.randint(1,6)
print('Well done '+str(name)+'. You are the winner!')
I have to be able to generate a random name.
The Python "SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?" occurs when we try to assign to a literal (e.g. a string or a number). To solve the error, specify the variable name on the left and the value on the right-hand side of the assignment.
A string literal can be created by writing a text(a group of Characters ) surrounded by the single(”), double(“”), or triple quotes. By using triple quotes we can write multi-line strings or display in the desired way.
Python's True and False values (and None ) are not really literals, but predefined names for those values. But the concept is similar: Python is giving you a way to write those values directly into your code. Nearly all programming languages have literals for most of their frequently-used types.
String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello".
The left hand side of the =
operator needs to be a variable. What you're doing here is telling python: "You know the number one? Set it to the inputted string.". 1
is a literal number, not a variable. 1
is always 1
, you can't "set" it to something else.
A variable is like a box in which you can store a value. 1
is a value that can be stored in the variable. The input
call returns a string, another value that can be stored in a variable.
Instead, use lists:
import random
namelist = []
namelist.append(input("Please enter name 1:")) #Stored in namelist[0]
namelist.append(input('Please enter name 2:')) #Stored in namelist[1]
namelist.append(input('Please enter name 3:')) #Stored in namelist[2]
namelist.append(input('Please enter name 4:')) #Stored in namelist[3]
namelist.append(input('Please enter name 5:')) #Stored in namelist[4]
nameindex = random.randint(0, 5)
print('Well done {}. You are the winner!'.format(namelist[nameindex]))
Using a for loop, you can cut down even more:
import random
namecount = 5
namelist=[]
for i in range(0, namecount):
namelist.append(input("Please enter name %s:" % (i+1))) #Stored in namelist[i]
nameindex = random.randint(0, namecount)
print('Well done {}. You are the winner!'.format(namelist[nameindex]))
Just adding 1 more scenario which may give the same error:
If you try to assign values to multiple variables, then also you will receive same error. For e.g.
In C (and many other languages), this is possible:
int a=2, b=3;
In Python:
a=2, b=5
will give error:
can't assign to literal
EDIT:
As per Arne's comment below, you can do this in Python for single line assignments in a slightly different way:
a, b = 2, 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With