(1)缩进错误
演示代码:
>>> if 5>3: print('5>3') SyntaxError: expected an indented block
>>> for i in range(5): print(i) SyntaxError: expected an indented block
错误原因分析与解决方案:
Python代码对缩进的要求非常严格,代码缩进层级决定了代码的所属关系。Python初学者最容易遇到的错误应该就是缩进错误,遇到这样的错误时,要仔细检查代码中的缩进是否与预定义的功能逻辑相符。
(2)成员访问错误
演示代码:
>>> x = [1, 2, 3] >>> x.add(4) Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> x.add(4) AttributeError: 'list' object has no attribute 'add'
>>> x = {1, 2, 3} >>> x.count(3) Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> x.count(3) AttributeError: 'set' object has no attribute 'count'
错误原因分析与解决方案:
错误信息显示当前对象并不具有一个叫做'***'的属性或方法,所以调用失败。这种错误一般是因为记错了对象属性或方法,也可能是前面某段代码代码修改了变量x的类型,自己却忘记了。遇到这种错误时,首先应使用type()函数确定当前位置的x是什么类型,然后可以在使用dir()确定该类型的对象是否具有'***'属性或方法。
(3)误用函数或方法返回值
演示代码:
>>> x = [1, 3, 2] >>> x = x.sort() >>> x.remove(3) Traceback (most recent call last): File "<pyshell#28>", line 1, in <module> x.remove(3) AttributeError: 'NoneType' object has no attribute 'remove'
错误原因分析与解决方案:
这种错误比较隐蔽一些,表面看上去好像是某个类型的对象不具有某某某属性,而实际上是函数或方法的误用。在Python中,如果函数或方法没有返回值,则认为其返回控制None。不过,这种错误又比较明显,因为一般是'NoneType' object has no attribute......,这里的'NoneType'是个很好的提示。遇到这种错误时,需要仔细检查出现问题的代码之前的函数调用或方法调用。
(4)试图删除或修改不可变容器对象中的元素值
演示代码:
>>> x = (1, 2, 3) >>> del x[1] Traceback (most recent call last): File "<pyshell#36>", line 1, in <module> del x[1] TypeError: 'tuple' object doesn't support item deletion >>> x[1] = 4 Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> x[1] = 4 TypeError: 'tuple' object does not support item assignment
>>> x = 'hello world' >>> x[0] = 'w' Traceback (most recent call last): File "<pyshell#41>", line 1, in <module> x[0] = 'w' TypeError: 'str' object does not support item assignment
错误原因分析与解决方案:
在自己编写代码时,一般并不会发生这样的错误,因为作为Python程序员我们肯定知道元组和字符串是不可变的容易对象,是不允许修改其中元素值的,也不允许删除其中的元素。遇到这种错误时,一般是调用了其他函数或方法而不知道该函数或方法返回的是元组或字符串或其他不可变容易对象,应仔细检查出现错误的代码之前的函数或方法调用代码。
(5)下标或键错误
演示代码:
>>> x = {'a':97, 'b':98, 'C':67} >>> x['d'] Traceback (most recent call last): File "<pyshell#46>", line 1, in <module> x['d'] KeyError: 'd' >>> x = {1, 2, 3, 4} >>> x[2] Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> x[2] TypeError: 'set' object does not support indexing
>>> x[0:3] Traceback (most recent call last): File "<pyshell#52>", line 1, in <module> x[0:3] TypeError: 'set' object is not subscriptable >>> x = [1, 2, 3, 4] >>> x[5] Traceback (most recent call last): File "<pyshell#50>", line 1, in <module> x[5] IndexError: list index out of range
错误原因分析与解决方案:
在Python中,列表、元组、字符串和range对象支持使用整数作为下标去访问指定位置上的元素值;列表支持使用整数作为下标去修改指定位置上的元素值;字典支持使用指定的键去访问或修改对应的值。当如果下标或键不存在则会抛出异常。集合不支持使用下标或键访问其中的元素。map、filter、enumerate、zip等对象也不支持使用下标访问其中的元素值。
(6)切片不连续错误
演示代码:
>>> x = list(range(6)) >>> x [0, 1, 2, 3, 4, 5] >>> x[::2] = [1] Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> x[::2] = [1] ValueError: attempt to assign sequence of size 1 to extended slice of size 3
错误原因分析与解决方案:
在使用切片操作修改列表中元素值时,如果切片不连续(也就是abs(step)>1),则等号两侧必须含有同样多的元素数量。切片连续时并没有这个限制。