input()和raw_input()区别

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函数。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值