找到你要的答案

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

Q:什么是错的,有人可以帮我输入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??

我在Python初学者。这似乎很基本。我使用Python 2.7

如果我使用Python提示符输入,我得到一个错误。我正在打字:

my_reply= input ("Enter your reply")

如果我输入任何字母,我得到一个错误的说法,my_reply未定义。但是,如果我输入一个数字,没有错误。

我做错什么了??

answer1: 回答1:

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.

在Python 2.7,输入实际上是试图评估“串”,你通过它。因此,如果你要输入类似的东西

Hello World

它只会对Hello World的表达式进行评估-而不是字符串。这可以通过提供正确的Python字符串避免:

'Hello World'

真正的解决方案是使用raw_input功能。raw_input并不试图评估价值,因此,第一个例子会象预期的那样工作。

answer2: 回答2:

In Python2, you should use raw_input rather than input.

在python2,你应该使用raw_input而不是输入。

answer3: 回答3:

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

你应该使用raw_input所以Python不会评价你的输入为一个字符串。这是你的责任,以验证输入。

python  python-2.7  input