input()和raw_input()区别
2015-07-01 17:06
567 查看
input的源码:
def input(prompt):
return eval(raw_input(prompt))
看个例子:
>>> user = raw_input('enter your name \n')
enter your name
liubei
>>> user
'liubei'
>>> user = input('enter your name \n')
enter your name
zhaoyun
提示error:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
user = input('enter your name \n')
File "<string>", line 1, in <module>
NameError: name 'zhaoyun' is not defined
raw_input()可以把把任何的用户输入转换为字符串存储。
input()则不会自动转换。必须输入符合要求的字符串。因为input总是假定用户输入的是合法的pytho表达式。
>>> user = input('enter your name \n')
enter your name
'lvbu' #以字符串作为输入
>>> user
'lvbu'
当输入为字符串表达式时:input会计算字符串中的数字表达式。 raw_iput不会,直接输入字符串表达式。如下:
>>> count = input()
57+3
>>> count
60
>>> count = raw_input()
57+3
>>> count
'57+3'
>>>
当输入为纯数字时,input返回的是数值类型,如int,float. raw_input返回的是字符串类型,string类型。如下:
>>> user = input('enter the number')
enter the number123456789
>>> user
123456789
>>> user = raw_input('enter the number')
enter the number123456789
>>> user
'123456789'
>>>
提示:除法对有input有特殊要求,否则应该尽可能使用raw_input函数。
def input(prompt):
return eval(raw_input(prompt))
看个例子:
>>> user = raw_input('enter your name \n')
enter your name
liubei
>>> user
'liubei'
>>> user = input('enter your name \n')
enter your name
zhaoyun
提示error:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
user = input('enter your name \n')
File "<string>", line 1, in <module>
NameError: name 'zhaoyun' is not defined
raw_input()可以把把任何的用户输入转换为字符串存储。
input()则不会自动转换。必须输入符合要求的字符串。因为input总是假定用户输入的是合法的pytho表达式。
>>> user = input('enter your name \n')
enter your name
'lvbu' #以字符串作为输入
>>> user
'lvbu'
当输入为字符串表达式时:input会计算字符串中的数字表达式。 raw_iput不会,直接输入字符串表达式。如下:
>>> count = input()
57+3
>>> count
60
>>> count = raw_input()
57+3
>>> count
'57+3'
>>>
当输入为纯数字时,input返回的是数值类型,如int,float. raw_input返回的是字符串类型,string类型。如下:
>>> user = input('enter the number')
enter the number123456789
>>> user
123456789
>>> user = raw_input('enter the number')
enter the number123456789
>>> user
'123456789'
>>>
提示:除法对有input有特殊要求,否则应该尽可能使用raw_input函数。
相关文章推荐
- 适配较好的写法
- 题目1008:最短路径问题
- iOS开发之iOS程序偏好设置(Settings Bundle)的使用
- BaiduMap---百度地图官方Demo之基本地图功能(创建一张百度地图并管理地图的生命周期)
- 操作远程计算机文件
- iOS开发笔记24--第三方库JSONModel的使用
- XGBoost:参数解释
- 从github上导入dubbo项目
- Rails应用开发点滴
- Entityframework codefirst 数据库变更
- MQTT的学习研究(六) MQTT moquette 的 Blocking API 订阅消息客户端使用
- 登录织梦后台成功后又返回登录页面
- 轻松突破GRE阅读难句在于掌握语法结构
- ios数据库应用高级编程
- 【实用】Excel表格技巧合集,让你工作效率翻一倍!
- struts.xml的加载路径及配置问题
- 给openwrt移植一个其他项目的问题记录
- IE7,8,9兼容性处理
- MQTT的学习研究(五) MQTT moquette 的 Blocking API 发布消息服务端使用
- wifi中的ap、sta、ap+sta模式的含义