转自: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:
>>> user
''
>>> user = input("Enter your name,please!")
Enter your name,please!
>>> user >>> 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 , in <module>
user = input("Enter your name,please!")
File "<string>", line , 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 , in <module>
user = input("Enter your name,please!")
File "<string>", line , in <module>
NameError: name 'scr' is not defined
>>>

python input 与raw_input函数的区别的更多相关文章

  1. Python中input()和raw_input()函数的区别

    问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...

  2. python中input和raw_input函数

    python input() 相等于 eval(raw_input(prompt)) ,用来获取控制台的输入. raw_input() 将所有输入作为字符串看待,返回字符串类型.而 input() 在 ...

  3. Python input 和 raw_input的区别

    转载[http://www.pythonclub.org/python-basic/input] 使用input 和 raw_input 都可以读取控制台的输入,但是input和raw_input在处 ...

  4. python input() 与 raw_input()

    使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的 当输入为纯数字时: input返回的是数值类型,如int,floatraw_inpo ...

  5. python input() 与raw_input()

    使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的1:纯数字输入 当输入为纯数字时 input返回的是数值类型,如int,float   ...

  6. Python——input与raw_input的区别

      区别一: raw_input():python2版本 input():python3版本 区别二: raw_input()不管是输数字还是字符串,结果都会以字符串的形式展现出来 input()则是 ...

  7. Python input()和raw_input()的区别

    当输入为数字的时候,input()获得的是数字,而后者获得的是str,可以用int(raw_input())来转换. i = input() print i+1 j = raw_input() pri ...

  8. Python input和print函数

    一.input函数 可以看出,input()函数默认输入的是字符串类型,需要eval()函数将其进行转换. 区别直接赋值的情况,Python可以自动识别数据类型 二.print函数 1.直接输出 无论 ...

  9. python:input()和raw_input()

    1.input() 接受各种合法类型的输入,比如输入字符串,则需要使用双引号,否则报错. input()会自动判断类型,比如输入的是 1.1,则返回的类型是float. 示例: 2.raw_input ...

随机推荐

  1. 转 MySQL连接超时

    在负载较重的MySQL服务器上,有时你偶尔会看到一些连接超时的错误,诸如: Can’t connect to MySQL server on ‘mydb’(110).如果当时你有多个连接请求,你会发现 ...

  2. bzoj 2466 异或方程组

    对于每个灯,我们用一个变量表示其决策,xu=0表示不选,xu=1表示选.因为每个灯最后必须都亮,所以每个等都对应一个异或方程. 解这个异或方程组,有几种情况: 1.存在唯一解(得到的上三角系数矩阵的主 ...

  3. python3登录网页(163邮箱)实例

    # -*- coding: utf-8 -*- import urllibimport http.cookiejar as cookielibimport urllib.request as urll ...

  4. hihocoder1322希尔伯特曲线(163周)

    hihocoder1322 : 希尔伯特曲线(163周) 题目链接 思路: 看图,对每个Hn迭代到H(n-1) 直到迭代到1就ok,判断在哪个区间就好了.一定一定要注意数据的范围!! ac代码: // ...

  5. ROS知识(9)----安装Turtlebot2和远程控制Turtlebot2

    安装turtlebot2,场景为:turtlebot2上搭载着一台电脑主机A,该电脑作为主机Master,有自带的电源和3D传感器,roscore在该台机器上启动.pc电脑远程连接A,和A通讯,pc不 ...

  6. PID DC/DC Converter Controller Using a PICmicro Microcontroller

    http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011794 ...

  7. 转:如何解决“My mac 64-bit”问题

    童鞋们都知道Xcode会根据当前SDK在Run按钮旁边的选项栏中显示适合的Simulator供开发者选择,如下图: 但是有时候则错误显示“My mac 64-bit” ,这个明显不是我们想要的,如下图 ...

  8. java代码将e.printStackTrace()写入log4j文件异常信息

    try { ... } catch (Exception e) { log.error( "failed!", e ); } 或者 try { ... } catch (Excep ...

  9. java 字符串,字符数组,list间的转化

    1.关于java.lang.string.split xxx.split()方法可以将一个字符串分割为子字符串,然后将结果作为字符串数组返回. 2.字符串转字符数组 String str =" ...

  10. IOS tableview 横向滚动

    1. UITableView 设置 CGRect tableViewRect = CGRectMake(0.0, 0.0, 50.0, 320.0);self.tableView = [[UITabl ...