Python 3中raw_input()和input()的区别是什么?
当前回答
在python3中,raw_input()不存在,Sven已经提到过。
在Python 2中,input()函数计算输入值。
例子:
name = input("what is your name ?")
what is your name ?harsha
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
name = input("what is your name ?")
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
在上面的例子中,Python 2。X试图将harsha作为变量而不是字符串来计算。为了避免这种情况,我们可以在输入后使用双引号,如“harsha”:
>>> name = input("what is your name?")
what is your name?"harsha"
>>> print(name)
harsha
raw_input ()
raw_input() '函数不会求值,它只会读取你输入的内容。
例子:
name = raw_input("what is your name ?")
what is your name ?harsha
>>> name
'harsha'
例子:
name = eval(raw_input("what is your name?"))
what is your name?harsha
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
name = eval(raw_input("what is your name?"))
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
在上面的例子中,我只是试图用eval函数计算用户输入的值。
其他回答
区别在于raw_input()在Python 3中不存在。X,而input()是。实际上,旧的raw_input()已经被重命名为input(),旧的input()已经没有了,但是可以使用eval(input())轻松地模拟。(记住eval()是邪恶的。如果可能的话,尽量使用更安全的方式解析输入。)
如果你想确保你的代码使用python2和python3运行,在脚本的开头添加input()函数:
from sys import version_info
if version_info.major == 3:
pass
elif version_info.major == 2:
try:
input = raw_input
except NameError:
pass
else:
print ("Unknown python version - input function not safe")
在python3中,raw_input()不存在,Sven已经提到过。
在Python 2中,input()函数计算输入值。
例子:
name = input("what is your name ?")
what is your name ?harsha
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
name = input("what is your name ?")
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
在上面的例子中,Python 2。X试图将harsha作为变量而不是字符串来计算。为了避免这种情况,我们可以在输入后使用双引号,如“harsha”:
>>> name = input("what is your name?")
what is your name?"harsha"
>>> print(name)
harsha
raw_input ()
raw_input() '函数不会求值,它只会读取你输入的内容。
例子:
name = raw_input("what is your name ?")
what is your name ?harsha
>>> name
'harsha'
例子:
name = eval(raw_input("what is your name?"))
what is your name?harsha
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
name = eval(raw_input("what is your name?"))
File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined
在上面的例子中,我只是试图用eval函数计算用户输入的值。
我想在大家为python 2用户提供的解释基础上再补充一点细节。Raw_input(),到目前为止,您已经知道它计算用户作为字符串输入的任何数据。这意味着python甚至不会试图再次理解输入的数据。它所考虑的是输入的数据将是字符串,无论它是否是一个实际的字符串或int或任何东西。
而input()则试图理解用户输入的数据。因此,像helloworld这样的输入甚至会显示错误为“helloworld is undefined”。
总之,对于python2,要输入一个字符串,你需要像'helloworld'这样输入,这是python中使用字符串的常见结构。
在Python 2中,raw_input()返回一个字符串,input()尝试将输入作为Python表达式运行。
由于获取字符串几乎总是你想要的,Python 3通过input()实现了这一点。正如Sven所说,如果你想要旧的行为,eval(input())是可行的。
推荐文章
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本
- 我在哪里调用Keras的BatchNormalization函数?
- 打印测试执行时间并使用py.test锁定缓慢的测试
- 插入一行到熊猫数据框架
- 要列出Pandas DataFrame列
- 在Django模型中存储电话号码的最佳方法是什么?
- 从导入的模块中模拟函数
- 滚动或滑动窗口迭代器?
- python的方法找到最大值和它的索引在一个列表?
- 如何读取文件的前N行?
- 如何删除matplotlib中的顶部和右侧轴?
- 解析.py文件,读取AST,修改它,然后写回修改后的源代码
- Visual Studio Code:如何调试Python脚本的参数
- 使用元组/列表等等。从输入vs直接引用类型如list/tuple/etc