您的位置:首页 > 其它

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

2016-07-19 18:50 302 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: