Python Interpreter: Can't assign to literal [closed]
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?
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.
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()
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)
精彩评论