Python 解释器:无法分配给文字
我编写了一个包含以下代码的程序:
('<') = raw_input(v1), ('>') = raw_input(v2)
并收到消息 syntax error:无法分配给文字
。我做错了什么?
I wrote a program with this code in it:
('<') = raw_input(v1), ('>') = raw_input(v2)
and recieve the message syntax error: can't assign to literal
. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试将用户输入的内容分配给字符串的实例。那是行不通的。
要在另一个答案的评论中抓住您的问题并尝试合并它,如果您希望能够输入
v1
并让它返回'<'
,您需要为此:听起来,在进一步深入之前,您强烈需要了解一些基本的编程概念,例如赋值、变量和函数。
You're attempting to assign something the user enters to an instance of a String. That doesn't work.
To grab your question in a comment on another answer and attempt to incorporate it, if you want to be able to type
v1
and have it return'<'
, you need to do this:It sounds like, before you go much further, you strongly need to work through some of the basic programming concepts like assignment, variables, and functions.
简单说明一下您的意思:
您所说的是(忽略
v1
和v2
变量) :用清晰的英语输入这些行,您是在说:
“将
'<'
设置为将raw_input()
中的用户定义值分配给元组raw_input(), '>'
”。“将某个固定值设置为等于用户提供的值”在代数上相当于“将 5 设置为等于上一个方程的值”。
*
由于逗号运算符是绑定最少的运算符之一,因此您实际上是在设置由字符串raw_input(), '>'元组 code> 等于第二个
raw_input
调用中的字符串。该声明可以细分如下:
raw_input(), '>' = raw_input()
解释为:To take a stab at what you mean:
What you are saying is (ignoring the
v1
andv2
variables):Typing those lines out in legible English, you are saying:
"Set
'<'
to be the result of assigning a user-defined value fromraw_input()
to the tupleraw_input(), '>'
".Saying, "Set some fixed value to be equal to the user-provided value" is the algebraic equivalent of saying "Set 5 to be equal to the value of the previous equation."
*
Since the comma operator is one of the least binding operators, you are actually setting the tuple composed of the stringsraw_input(), '>'
to be equal to the string from the secondraw_input
call.The statement can be broken down as follows:
raw_input(), '>' = raw_input()
is interpreted as:您是否尝试使用
<
和>
作为提示来获取输入?如果是这样,这就是您应该做的:raw_input
将提示作为参数,并且此函数调用的输出(您在终端中输入的内容)被分配给v1< /code> 和
v2
。一行中的另一个选项,因为看起来您正在尝试执行一行:
您收到该错误消息的原因是
('<')
就是所谓的文字。文字是在代码中显式键入的值。基本上,不是一个变量。这就像说3 = len(mylist)
... 如何将len
函数的输出分配给 3?你不能,因为 3 不是变量。您应该只分配给一个变量,在 python(和大多数其他语言)中,通常是某种类似于单词的字符集,例如v1
或myinput
:Are you trying to get input with
<
and>
as the prompts? If so, this is what you should be doing:raw_input
takes in the prompt as a parameter, and the output of this function call (what you type in the terminal) gets assigned intov1
andv2
.Another option in one line, since it looks like you are trying to do one line:
The reason you are getting that error message is
('<')
is what is called a literal. A literal is a value that is explicitly typed out in your code. Basically, not a variable. This is like saying3 = len(mylist)
... How do you assign the output of thelen
function to 3? You can't, because 3 is not a variable. You should only be assigning into a variable, in python (and most other languages) typically some sort of word-like set of characters, likev1
ormyinput
: