Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 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

I know this has been a while, but your answer really helped me today as I was trying to do multiple assignments in the same line. Why is multiple assignment allowed but this is not? Thanks – Bowen Liu Aug 12, 2018 at 21:08 @BowenLiu Python allows you to assign one value to many variables a=b=3 which sets a=3 and b=3. So the statement a=2, b=5 will try to set a=5 (works) and 2,b=5 (doesn't work). You can neither unpack 5 (since 5 isn't iterable) nor can you assign anything to a literal 2. – Robert Aug 13, 2020 at 11:34

You are trying to assign to literal integer values. 1, 2, etc. are not valid names; they are only valid integers:

>>> 1 = 'something' File "<stdin>", line 1 SyntaxError: can't assign to literal

You probably want to use a list or dictionary instead:

names = []
for i in range(1, 6):
    name = input("Please enter name {}:".format(i))
    names.append(name)

Using a list makes it much easier to pick a random value too:

winner = random.choice(names)
print('Well done {}. You are the winner!'.format(winner))

1, 2, 3 ,... are invalid identifiers in python because first of all they are integer objects and secondly in python a variable name can't start with a number.

>>> 1 = 12    #you can't assign to an integer
  File "<ipython-input-177-30a62b7248f1>", line 1
SyntaxError: can't assign to literal
>>> 1a = 12   #1a is an invalid variable name
  File "<ipython-input-176-f818ca46b7dc>", line 1
    1a = 12
SyntaxError: invalid syntax

Valid identifier definition:

identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"

This is taken from the Python docs:

Identifiers (also referred to as names) are described by the following lexical definitions:
identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"
Identifiers are unlimited in length. Case is significant.

That should explain how to name your variables.

one = input("Please enter name one:") two = input('Please enter name two:') three = input('Please enter name three:') four = input('Please enter name four:') five =input('Please enter name five:') name=random.randint(1,5) name = str(name) if name == "1": name = one print('Well done '+ name +'. You are the winner!') elif name == "2": name = two print('Well done '+ name +'. You are the winner!') elif name == "3": name = three print('Well done '+ name +'. You are the winner!') elif name == "4": name = four print('Well done '+ name +'. You are the winner!') else: name = five print('Well done '+ name +'. You are the winner!')

I got the same error: SyntaxError: can't assign to literal when I was trying to assign multiple variables in a single line.

I was assigning the values as shown below:

    score = 0, isDuplicate = None

When I shifted them to another line, it got resolved:

    score = 0
    isDuplicate = None

I don't know why python does not allow multiple assignments at the same line but that's how it is done.

There is one more way to asisgn it in single line ie. Separate them with a semicolon in place of comma. Check the code below:

score = 0 ; duplicate = None
                Python does allow assigning mutliple values in a single line, you just have to write it differently: ` score, isDuplicate = 0, None`.
– Arne
                Apr 9, 2019 at 7:39
                @Arne, how does it work for three vars, this will not work; max_gap,gap_counter = 0, gap_started = False,0
– Timo
                Sep 21, 2020 at 6:58
                @Timo left side of the assignment operator go the variable names, right side of the assignment operator go the values. so, max_gap,gap_counter, gap_started = 0, 0, False. only one assignment operator is allowed per line, barring shenanigans with ;.
– Arne
                Sep 21, 2020 at 11:31