python input 与raw_input函数的区别
2012-07-26 18:04
465 查看
以前没有深入考虑过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
>>>
>>> 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
>>>
相关文章推荐
- Python2.7中input和raw_input函数有什么区别
- python input 与raw_input函数的区别
- python中的input函数和raw_input函数的区别
- python input函数和raw_input函数的区别
- python input 与raw_input函数的区别
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
- python input 和raw_input 区别
- python中input和raw_input有什么区别
- 【python】讨论input 和raw_input 的区别
- python中input和raw_input区别(原创)
- python中input()与raw_input()的区别分析
- Python raw_input和input总结 在版本2和版本3中的区别
- Python中的input方法与raw_input方法区别小记
- python中input()和raw_input()的区别
- Python中input()和raw_input()函数的区别
- python中raw_input 和 input的区别
- python 笔记 raw_input()与input()的区别——12.24
- Python input()和raw_input()的区别
- python中raw_input()以及input()函数的区别
- python 学习(一) 中文,字符串,以及input和raw_input区别