Python:不能分配给文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18716564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: can't assign to literal
提问by Stacey J
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
.
我的任务是编写一个程序,要求用户输入它存储在列表中的 5 个名称。接下来,它随机选择这些名字中的一个,并宣布该人为获胜者。唯一的问题是,当我尝试运行它时,它显示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.
我必须能够生成一个随机名称。
采纳答案by Manishearth
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.
=
运算符的左侧需要是一个变量。你在这里做的是告诉python:“你知道第一名吗?将它设置为输入的字符串。”。1
是一个文字数字,而不是一个变量。1
总是1
,您不能将其“设置”为其他内容。
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.
变量就像一个盒子,您可以在其中存储值。1
是一个可以存储在变量中的值。该input
调用返回一个字符串,可以存储在一个变量另一个值。
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:
使用 for 循环,你可以减少更多:
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]))
回答by Ashwini Chaudhary
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, 2, 3 ,... 在python中是无效的标识符,因为首先它们是整数对象,其次在python中变量名不能以数字开头。
>>> 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"
回答by Martijn Pieters
You are trying to assign to literal integer values. 1
, 2
, etc. are not valid names; they are only valid integers:
您正在尝试分配给文字整数值。1
、2
等不是有效名称;它们只是有效的整数:
>>> 1
1
>>> 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))
回答by Mohit Gupta
You should use variables to store the names.
您应该使用变量来存储名称。
Numbers can't store strings.
数字不能存储字符串。
回答by Eric
1
is a literal. name = value
is an assignment. 1 = value
is an assignment to a literal, which makes no sense. Why would you want 1
to mean something other than 1
?
1
是文字。name = value
是一个任务。1 = value
是对文字的赋值,这是没有意义的。你为什么要1
表达除1
?
回答by Joe Doherty
This is taken from the Python docs:
这取自 Python 文档:
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.
那应该解释如何命名变量。
回答by tryingToLearn
Just adding 1 more scenario which may give the same error:
只需再添加 1 个可能会出现相同错误的场景:
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:
在 C(和许多其他语言)中,这是可能的:
int a=2, b=3;
In Python:
在 Python 中:
a=2, b=5
will give error:
会报错:
can't assign to literal
不能分配给文字
EDIT:
编辑:
As per Arne'scomment below, you can do this in Python for single line assignments in a slightly different way:
a, b = 2, 5
根据下面Arne 的评论,您可以在 Python 中以稍微不同的方式为单行分配执行此操作:
a, b = 2, 5
回答by Shagun Pruthi
I got the same error: SyntaxError: can't assign to literalwhen I was trying to assign multiple variables in a single line.
我遇到了同样的错误:SyntaxError:当我试图在一行中分配多个变量时,无法分配给文字。
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.
我不知道为什么 python 不允许在同一行进行多个赋值,但这就是它的完成方式。
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