Search code examples
python-2.7raw-input

Does any assigned variable equal(==) to an "raw_input()" in Python2?


The "raw_input" is generally used for taking a respond of prompt to a string, and then it can be also assigned(=) by a variable.

But I found something strange(for me) and can't well understand. In a mistake incident case(see example below), I put a equal(==) for assigning variable in a function. Then I run it in interpreter and didn't have any error. I through my script was well done but it could not work as my expert.

My questions: I would like to know this usage of "raw_input" in Python is correct? If yes, how we use it? If not, why don't interpreter give us a error warning?

Thank you so much.

Example:

Python 2.7.3 (default, Apr 24 2012, 00:00:54) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> v = "Variable"
>>> def test():
...     v == raw_input(">")
...     print v
... 
>>> test()
>Hello!
Variable
>>> 

Solution

  • This:

    v == raw_input(">")
    

    is simply a comparison. You get True or False as a result and then throw it away, because you don't give it a name. You could write

    comparison = v == raw_input(">")
    print comparison
    

    to see the value.