Amin Elaghil
Amin Elaghil

Reputation: 1

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??

Upvotes: -1

Views: 87

Answers (3)

DeepSpace
DeepSpace

Reputation: 81594

You should use raw_input so Python won't evaluate your input as a string. It is then your responsibility to validate the input.

Upvotes: 1

Thomas Baruchel
Thomas Baruchel

Reputation: 7507

In Python2, you should use raw_input rather than input.

Upvotes: 1

mpolednik
mpolednik

Reputation: 1023

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.

Upvotes: 3

Related Questions