Python:can';不分配给文字
我的任务是编写一个程序,要求用户输入它存储在列表中的5个名称。接下来,它随机选择其中一个名字,并宣布此人为获胜者。唯一的问题是,当我尝试运行它时,它说Python:can';不分配给文字,python,python-3.x,Python,Python 3.x,我的任务是编写一个程序,要求用户输入它存储在列表中的5个名称。接下来,它随机选择其中一个名字,并宣布此人为获胜者。唯一的问题是,当我尝试运行它时,它说不能分配给literal 这是我的代码: 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:'
不能分配给literal
这是我的代码:
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!')
我必须能够生成一个随机名称。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
您正在尝试分配给文字整数值<代码>1、
2
等不是有效名称;它们仅为有效整数:
>>> 1
1
>>> 1 = 'something'
File "<stdin>", line 1
SyntaxError: can't assign to literal
使用列表也可以更轻松地选择随机值:
winner = random.choice(names)
print('Well done {}. You are the winner!'.format(winner))
您应该使用变量来存储名称
数字不能存储字符串。
1
是一个文本name=value
是一个赋值1=value
是对文本的赋值,没有意义。为什么希望1
表示的不是1
?这是从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.
这应该解释如何命名变量。操作符的左侧需要是一个变量。您在这里所做的是告诉python:“您知道数字1吗?将其设置为输入的字符串。”
1
是一个文字数字,不是变量1
始终是1
,您不能将其“设置”为其他内容
变量就像一个可以存储值的盒子1
是可存储在变量中的值。input
调用返回一个字符串,这是可以存储在变量中的另一个值
相反,请使用:
使用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]))
只需再添加一个可能会产生相同错误的场景: 如果您尝试将值分配给多个变量,那么您也将收到相同的错误。例如 在C语言(以及许多其他语言)中,这是可能的:
int a=2, b=3;
在Python中:
a=2, b=5
将给出错误:
无法分配给文字
编辑:
根据下面的注释,您可以在Python中以稍微不同的方式为单行赋值执行此操作:
a,b=2,5
当我试图在一行中分配多个变量时,我遇到了相同的错误:语法错误:无法分配给文字
我分配的值如下所示:
score = 0, isDuplicate = None
当我把他们换到另一条线上时,问题解决了:
score = 0
isDuplicate = None
我不知道为什么python不允许在同一行中进行多个赋值,但这就是它的实现方式
还有一种方法可以用单行表示,即用分号代替逗号分隔。检查以下代码:
score = 0 ; duplicate = None
我知道这已经有一段时间了,但是你的回答真的帮助了我,因为我尝试在同一行中做多个作业。为什么允许多次分配,但不允许?Thanks@BowenLiuPython允许您将一个值分配给多个变量
a=b=3
,这些变量设置a=3
和b=3
。因此,语句a=2,b=5
将尝试设置a=5
(工作)和2,b=5
(不工作)。您既不能解压缩5
(因为5
不可编辑),也不能为文本2
@Robert Gotcha分配任何内容。谢谢。Python确实允许在一行中分配多个值,您只需以不同的方式编写它:`score,isDuplicate=0,None`.@Arne,它对三个变量如何工作,这将不起作用max\u gap,gap\u counter=0,gap\u start=False,0
@Timo赋值运算符左侧转到变量名,赋值运算符右侧转到值。所以,max\u gap,gap\u计数器,gap\u start=0,0,False
。每行只允许一个赋值运算符,禁止使用进行欺骗代码>。好的,我也可以用分号来分隔变量初始化。
score = 0, isDuplicate = None
score = 0
isDuplicate = None
score = 0 ; duplicate = None
import random
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!')