【发布时间】:2018-01-09 15:50:39
【问题描述】:
由于 Python 2 的 raw_input 更改为仅用于 Python 3 的 input,我想知道是否有办法在考虑 Python 2 和 3 的同时接受输入。我正在尝试为两者编写脚本版本,并且这个输入部分是唯一不能正常工作的部分。
我尝试使用 Py2 仅运行 input,结果出现了这种情况:
>>> a = input('Input: ')
inp: test
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a = input('Input: ')
File "<string>", line 1, in <module>
NameError: name 'test' is not defined
我看到了引用输入的解决方法:
>>> a = input('Input: ')
inp: "testing test"
a
'testing test'
有没有办法将引号连接到输入的开头和结尾? '"' + input('input: ') + '"' 不工作
【问题讨论】:
-
永远不要使用 Python 2
input,而是像在欺骗中那样做
标签: python-3.x python-2.7