Python 解释器:无法分配给文字

发布于 2024-12-06 03:24:39 字数 152 浏览 1 评论 0原文

我编写了一个包含以下代码的程序:

('<') = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光沙漏 2024-12-13 03:24:39

我做错了什么?

您正在尝试将用户输入的内容分配给字符串的实例。那是行不通的。

要在另一个答案的评论中抓住您的问题并尝试合并它,如果您希望能够输入 v1 并让它返回 '<',您需要为此:

v1 = '<'

听起来,在进一步深入之前,您强烈需要了解一些基本的编程概念,例如赋值、变量和函数。

What am I doing wrong?

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:

v1 = '<'

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.

少跟Wǒ拽 2024-12-13 03:24:39

简单说明一下您的意思:

user_provided_value = raw_input("Say something:")

if user_provided_value == "v1":
    print "Heavier than a duck!"
elif user_provided_value == "v2":
    print "Lighter than a duck!"
else:
    print "You must enter either v1 or v2"

您所说的是(忽略 v1v2 变量) :

('<')              #1 Set '<'
                   #2 [ ('<') is the same as simply saying '<' ]
 =                 #3 to be the result of assigning
                   #5 to a tuple composed of
 raw_input()       #6 what the user types in at the prompt
 ,                 #7 (the comma operator creates a tuple)
 ('>')             #8 And '>'
 =                 # to be 
 raw_input()       #4 what the user types in at the prompt

用清晰的英语输入这些行,您是在说:

“将 '<' 设置为将 raw_input() 中的用户定义值分配给元组raw_input(), '>'”。

“将某个固定值设置为等于用户提供的值”在代数上相当于“将 5 设置为等于上一个方程的值”。


* 由于逗号运算符是绑定最少的运算符之一,因此您实际上是在设置由字符串 raw_input(), '>'元组 code> 等于第二个 raw_input 调用中的字符串。

该声明可以细分如下:

设置字符串“<”是通过评估语句 raw_input(), '>' 得到的值= raw_input()

raw_input(), '>' = raw_input() 解释为:

设置调用raw_input()和'>'的结果组成的元组等于调用 raw_input() 的结果

To take a stab at what you mean:

user_provided_value = raw_input("Say something:")

if user_provided_value == "v1":
    print "Heavier than a duck!"
elif user_provided_value == "v2":
    print "Lighter than a duck!"
else:
    print "You must enter either v1 or v2"

What you are saying is (ignoring the v1 and v2 variables):

('<')              #1 Set '<'
                   #2 [ ('<') is the same as simply saying '<' ]
 =                 #3 to be the result of assigning
                   #5 to a tuple composed of
 raw_input()       #6 what the user types in at the prompt
 ,                 #7 (the comma operator creates a tuple)
 ('>')             #8 And '>'
 =                 # to be 
 raw_input()       #4 what the user types in at the prompt

Typing those lines out in legible English, you are saying:

"Set '<' to be the result of assigning a user-defined value from raw_input() to the tuple raw_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 strings raw_input(), '>' to be equal to the string from the second raw_input call.

The statement can be broken down as follows:

Set the string '<' to be the value resulting from evaluating the statement raw_input(), '>' = raw_input()

raw_input(), '>' = raw_input() is interpreted as:

Set the tuple composed of the results of calling raw_input() and '>' to be equal to the results of calling raw_input()

榕城若虚 2024-12-13 03:24:39

您是否尝试使用 <> 作为提示来获取输入?如果是这样,这就是您应该做的:

v1 = raw_input('<')
v2 = raw_input('>')

raw_input 将提示作为参数,并且此函数调用的输出(您在终端中输入的内容)被分配给 v1< /code> 和 v2

一行中的另一个选项,因为看起来您正在尝试执行一行:

v1, v2 = raw_input('<'), raw_input('>')

您收到该错误消息的原因是 ('<') 就是所谓的文字。文字是在代码中显式键入的值。基本上,不是一个变量。这就像说 3 = len(mylist)... 如何将 len 函数的输出分配给 3?你不能,因为 3 不是变量。您应该只分配给一个变量,在 python(和大多数其他语言)中,通常是某种类似于单词的字符集,例如 v1myinput

v1 = len(mylist)

Are you trying to get input with < and > as the prompts? If so, this is what you should be doing:

v1 = raw_input('<')
v2 = raw_input('>')

raw_input takes in the prompt as a parameter, and the output of this function call (what you type in the terminal) gets assigned into v1 and v2.

Another option in one line, since it looks like you are trying to do one line:

v1, v2 = raw_input('<'), raw_input('>')

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 saying 3 = len(mylist)... How do you assign the output of the len 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, like v1 or myinput:

v1 = len(mylist)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文