先看一段操作:
[13:51 t /tmp]$ python
Python 2.7.11 (default, Mar 31 2016, 20:46:51)
[GCC 5.3.1 20151207 (Red Hat 5.3.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> name=input("Your name:")
Your name:talen #使用input()输入内容不加引号
Traceback (most recent call last):
File "
", line 1, in
File "", line 1, in
NameError: name 'talen' is not defined
>>> name=input("Your name:")
Your name:"talen" #使用input()输入内容添加引号
>>> print name
talen
>>> name=raw_input("Your name:")
Your name:talen #使用raw_input()输入内容不加引号
>>> print name
talen
>>>
从上面的例子可以看出,如果想输入字符串,input()的输入内容必须使用''号,否则会被认为是变量名。
>>> num=input("Your num:")
Your num:897
>>> type(num)
>>> num2=raw_input("Your num:")
Your num:897
>>> type(num2)
>>>
从这个操作来看,input()接受了输入的数据类型,raw_input()把输入当作字符串来看。所以input()输入必须进行数据类型的转换才能正确表达第一个例子中的输入。
input([prompt])
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically
valid, a will be raised. Other exceptions may be raised if
there is an error during evaluation.
If the module was loaded, then will use it to
provide elaborate line editing and history features.
Consider using the function for general input from users.
阅读(1571) | 评论(0) | 转发(0) |