python入门基础教程05 Pytho

发布时间:2019-06-25 09:57:28编辑:auto阅读(1325)

    Python-Shell反馈常见错误

    初学者通常会使用Python-Shell来学习Python基础及语法知识,在使用Python-Shell 时会遇到这样或者那样的错误,有的是语法错误,有的是键入的函数或者变量名字拼写错误,现就初学者常出现的错误做一个总结。

    变量、函数未定义

    下面我们简单总结一下在使用Python-Shell时常见的错误提示。

    >>>len = 12>>>le
    
    Traceback (most recent call last):
      File "<pyshell#36>", line 1, in <module>
        leNameError: name 'le' is not defined
    >>>

    上边Python-Shell反馈NameError: name ’le’ is not defined,是说’le’变量未定义,的确如此,因为之前我们赋值的是len变量等于12,le没有赋值就没有被创建故报错没有被定义。

    >>>len = "www.jeapedu.com">>> pint(len)
    
    Traceback (most recent call last):
      File "<pyshell#38>", line 1, in <module>
        pint(len)NameError: name 'pint' is not defined
    >>>

    从上边IDLE-Shell反馈NameError: name ’pint’ is not defined,可以看出pint 函数没有定义,应该是函数print少了个r字母。

    非语句字符

    在python语句指令里放入了一些非语句的字符,怎么理解?比如在print函数前敲了一个(多个)空格或者按了TAB 键,都会导致在Python-Shell里运行语句时出现错误。

    >>> s = 'www.jeapedu.com'>>> print s
    www.jeapedu.com
    >>>     print s
        
      File "<pyshell#45>", line 1
        print s
       ^
    IndentationError: unexpected indent
    >>>

    第4行错误的原因在于,print函数前有一个TAB或者若干个空格,导致在Shell里语法不合规而报错误。


关键字