转自: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 >>>
时间: 2024-11-10 07:45:02