input和raw_input的区别

input会假设用户输入的是合法的Python表达式
raw_input会把所有的输入当作原始数据,然后将其放入字符串中。

在最新的版本之中,input可以直接使用,替代了raw_input.

在2.7的版本中
>>> input('Enter you age: ')
Enter you age: kebi                                      input假设你输入的是字符串,但是字符串需要带引号啊
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'kebi' is not defined
直接输入数字会报错。
加上引号就可以了

 

>>> input('Enter you age: ')
Enter you age: 'kebi'
'kebi'                 加上引号就没事了

 

>>> raw_input('Enter you age: ')
Enter you age: kebi                     使用raw_input就不会存在这个问题了
'kebi'

在3.6的版本中
>>> raw_input('you name:')                         raw_input直接就不存在了,统一使用input
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined

>>> input('you name:')
you name:kebi
'kebi'

 

总结:不得不说这是一次进步。

posted @ 2017-10-19 10:14  明王不动心  阅读(1718)  评论(0编辑  收藏  举报