python中常见错误及解决方法

python常见的错误有

1.NameError变量名错误

2.IndentationError代码缩进错误

3.AttributeError对象属性错误

详细讲解

1.NameError变量名错误

报错:

>>> print a<br>Traceback (most recent call last):<br>File "<stdin>", line 1, in <module><br>NameError: name 'a' is not defined<br>

解决方案:

先要给a赋值。才能使用它。在实际编写代码过程中,报NameError错误时,查看该变量是否赋值,或者是否有大小写不一致错误,或者说不小心将变量名写错了。

注:在Python中,无需显示变量声明语句,变量在第一次被赋值时自动声明。

>>> a=1<br>>>> print a<br>1<br>

2.IndentationError代码缩进错误

代码

a=1b=2<br>if a<b:<br>print a<br>

报错:

IndentationError: expected an indented block<br>

原因:

缩进有误,python的缩进非常严格,行首多个空格,少个空格都会报错。这是新手常犯的一个错误,由于不熟悉python编码规则。像def,class,if,for,while等代码块都需要缩进。

缩进为四个空格宽度,需要说明一点,不同的文本编辑器中制表符(tab键)代表的空格宽度不一,如果代码需要跨平台或跨编辑器读写,建议不要使用制表符。

解决方案

a=1b=2<br>if a<b:<br>  print a<br>

3.AttributeError对象属性错误

报错:

>>> import sys<br>>>> sys.Path<br>Traceback (most recent call last):<br>File "<stdin>", line 1, in <module><br>AttributeError: 'module' object has no attribute 'Path'<br>

原因:

sys模块没有Path属性。

python对大小写敏感,Path和path代表不同的变量。将Path改为path即可。

>>> sys.path<br>['', '/usr/lib/python2.6/site-packages']<br>

初学者遇到的错误实例:

使用错误的缩进

Python用缩进区分代码块,常见的错误用法:

print('Hello!')
print('Howdy!')

导致:IndentationError: unexpected indent。同一个代码块中的每行代码都必须保持一致的缩进量

if spam == 42:
print('Hello!')
print('Howdy!')

导致:IndentationError: unindent does not match any outer indentation level。代码块结束之后缩进恢复到原来的位置

if spam == 42:
print('Hello!')

导致:IndentationError: expected an indented block,“:” 后面要使用缩进

变量没有定义

if spam == 42:
print('Hello!')

导致:NameError: name 'spam' is not defined

获取列表元素索引位置忘记调用 len 方法

通过索引位置获取元素的时候,忘记使用 len 函数获取列表的长度。

spam = ['cat', 'dog', 'mouse']
for i in range(spam):
print(spam[i])

导致:TypeError: range() integer end argument expected, got list. 正确的做法是:

spam = ['cat', 'dog', 'mouse']
for i in range(len(spam)):
print(spam[i])

当然,更 Pythonic 的写法是用 enumerate

spam = ['cat', 'dog', 'mouse']
for i, item in enumerate(spam):
print(i, item)

函数中局部变量赋值前被使用

someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()

导致:UnboundLocalError: local variable 'someVar' referenced before assignment

当函数中有一个与全局作用域中同名的变量时,它会按照 LEGB 的顺序查找该变量,如果在函数内部的局部作用域中也定义了一个同名的变量,那么就不再到外部作用域查找了。因此,在 myFunction 函数中 someVar 被定义了,所以 print(someVar) 就不再外面查找了,但是 print 的时候该变量还没赋值,所以出现了 UnboundLocalError

以上就是python中常见错误及解决方法的详细内容,更多关于python常见错误有哪些的资料请关注其它相关文章!

若文章对您有帮助,帮忙点个赞!

0
-5
发布时间 2020-06-21 09:00:34
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)