pythonpython-2.7input

Something is wrong, can somebody help me with INPUT in python


I am a beginner in python. This may seem very basic. I am using python 2.7

If I use input in python prompt, I get an error. I am typing this:

my_reply= input ("Enter your reply")

If I enter any letter/character, I get an error saying that my_reply is not defined. However, If I enter a number, there is no error.

What am I doing wrong??


Solution

  • In Python 2.7, input is actually trying to evaluate the "string" you pass to it. Therefore, if you were to input something like

    Hello World
    

    it would simply evaluate to the expression Hello World - not a string. This can be avoided by supplying correct python string:

    'Hello World'
    

    The real solution here is using raw_input function. raw_input does not try to evaluate the value and therefore, the first example would work as expected.