现在的位置: 首页 > 综合 > 正文

raw_input() 与 input() __ Python

2018年04月03日 ⁄ 综合 ⁄ 共 481字 ⁄ 字号 评论关闭

>>> input ("my age is : ")
my age is :23
23

>>> raw_input("my age is : ")
my age is : 23
'23'

有什么不一样?再看一个例子

>>> age = input(" how old r u ?")
how old r u ?23
>>> print "you are " + age + "!"

Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
    print "you are " + age + "!"
TypeError: cannot concatenate 'str' and 'int' objects

>>> age = raw_input("how old r u? ")
how old r u? 23
>>> print "you are " + age +"!"
you are 23!

input()是把读入的数据默认为Python expression(额...中文怎么说,python表达式?)

raw_input()是把读入的数据转换成String.

所以一般时候我们用来接受用户输入的时候,都是使用raw_input()而非input().

 

抱歉!评论已关闭.