【个人笔记】python input() xx is not defined

来源:互联网 发布:软件冗余技术 编辑:程序博客网 时间:2024/06/07 10:58

Eli:刚开始学python,慢慢记笔记。

----------------------------------------------------------------------------------------------------------------

 input()  xx is not defined

>>> spam = input()

101

>>> spam

'101'


在很多书里刚刚教input时都是这样写的。但是实际上运行起来。

>>> spam = input()

101

---------------------------------------------------------------

NameError

<python-input-xx-xxxxxxxxxxxxxx> in <module>()

----> 1 spam = input()

NameError: name '101' is not defined

>>>spam

---------------------------------------------------------------

NameError

<python-input-xx-xxxxxxxxxxxxxx> in <module>()

----> 1 spam = input()

NameError: name '101' is not defined


其实python希望读取一个合法的 python 表达式,即输入字符串的时候必须使用引号将它括起来。--高亮处英文

https://docs.python.org/2/library/functions.html#input

input([prompt])

Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is notsyntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.


下面又说了raw_input(),这个就符合我们刚开始想象中的输入方式:

>>> spam = raw_input()

101

>>> spam

'101'




原创粉丝点击