输入函数input()和raw_input()的区别

raw_input_A = raw_input("raw_input DIGIT: ")
print raw_input_A
input_B =input("input DIGIT: ")
print input_B
print  "INPUT :3 + %d = %d" % (input_B,input_B+3)

print  "RAW_INPUT :3 + %d = %d" % (raw_input_A,raw_input_A+3)

input()函数支持用户输入数字或者表达式,不支持输入字符串.返回的是数字类型的数值.
raw_input()函数捕获的是用户的原始输入,返回为字符串.如果需要用输入的数字计算,则需要使用int()函数转换一下.如果我们直接用输入的数值与某数想加,那么解释器就会报错:

TypeError: cannot concatenate ‘str’ and ‘int’ objects

输出结果:

raw_input DIGIT: 34
34
input DIGIT: 34
34
INPUT :3 + 34 = 37
Traceback (most recent call last):
  File "ex11.py", line 20, in <module>
    print  "RAW_INPUT :3 + %d = %d" % (raw_input_A,raw_input_A+3)
TypeError: cannot concatenate 'str' and 'int' objects
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值