以前没有深入考虑过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>>> user4>>> 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 "", line 1, in user = input("Enter your name,please!") File "", line 1, in 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 "", line 1, in user = input("Enter your name,please!") File "", line 1, in NameError: name 'scr' is not defined>>>