Python字符串输入会给出错误,除非带引号

问题描述 投票:0回答:2

我认为input方法将接受任何数字或字符串输入并将其打印出来。但是对于string,除非引用了string,否则它不起作用。为什么?

string input

python python-2.7
2个回答
2
投票

在Python 2.7中,input()将输入评估为代码,因此需要用引号引起来。 Python 2.7具有称为raw_input()的方法,该方法将所有输入视为字符串(不需要引号)。

在Python 3.x中,将Python 2.7 raw_input()方法重命名为input(),并且将python 2.7 input()功能替换为eval(input())

因此您可以在Python 2.7中使用raw_input()或切换到Python 3.x


0
投票

请确保您是在Python 3而不是Python 2上运行代码。在python 2上,您需要用“”

引用输入

尽管似乎对此有多种解决方案:

input() error - NameError: name '...' is not defined

© www.soinside.com 2019 - 2024. All rights reserved.