码迷,mamicode.com
首页 > 编程语言 > 详细

python input 与raw_input函数的区别

时间:2015-05-04 15:16:53      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

转自:http://blog.csdn.net/sruru/article/details/7790436

以前没有深入考虑过raw_input与input函数的区别,所以一直比较困惑,今天测试之后,有了较为深入了解,记录如下

>>> user = raw_input("Enter your name:")
Enter your name:scr
>>> user
scr
>>> user = raw_input("Enter your name:")
Enter your name:123
>>> user
123
>>> user = input("Enter your name,please!")
Enter your name,please!4
>>> user
4
>>> user = input("Enter your name,please!")
Enter your name,please!"scr"
>>> user
scr
>>> user = input("Enter your name,please!")
Enter your name,please!scr


Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
user = input("Enter your name,please!")
File "<string>", line 1, in <module>
NameError: name scr is not defined
>>>

总结:

raw_input更符合用户输入的习惯,把任何用户输入都转换成字符串存储,在需要其它类型的数据时,调用相应的函数进行转换;
input用户输入什么就存储什么,所以用户输入必须符合python语法要求,否则会出错,例如

>>> user = input("Enter your name,please!")
Enter your name,please!scr


Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
user = input("Enter your name,please!")
File "<string>", line 1, in <module>
NameError: name scr is not defined
>>>

 

python input 与raw_input函数的区别

标签:

原文地址:http://www.cnblogs.com/tv151579/p/4476116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!