python错误记录

312 阅读1分钟

1、SyntaxError: non-default argument follows default argument错误

这种错误原因是将没有默认值的参数在定义时放在了有默认值的参数的后面

2、IndentationError: expected an indented block

说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。

3、IndentationError: unexpected indent

python是一种对缩进非常敏感的语言 去掉该行前面的缩进

4、issing 2 required positional arguments: 'animal_

表示函数的实参数目不匹配

5、TypeError: build_profile() takes 2 positional arguments but 4 were given

函数实参个数与形参不符

6、AttributeError: 'dict' object has no attribute 'item'

发现是由于使用字典的时候把user_info.items()错写为user_info.item()

7、No module named 'pizza.py'; 'pizza' is not a package

Python导入模块名的时候,并不是导入文件名,而是模块名

8、module 'pizza' has no attribute 'make_pizza'

发现被导入的模块,需要先compile一下,才能被导入,否则报错

9、SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb8 in position 0: invalid start byte

带有中文的代码,会出现错误。

10、File "dog.py", line 2

SyntaxError: Non-UTF-8 code starting with '\xb4' in file dog.py on line 2,

o encoding declared; see python.org/dev/peps/pe… for details

导致出错的根源就是编码问题

首行增加,可解决问题。

coding=gbk

11、SyntaxError: invalid syntax

意思就是“语法错误:不正确的语法”。

12、TypeError: Dog() takes no arguments

出错原因是,在python中构造函数书写格式是__init__,而不是_init_,即在init两侧都是双下划线,不是单下划线。

应当是双下划线

13、AttributeError: 'Dog' object has no attribute 'name'

字典缺少属性'name'

14、NameError: name 'makeBuild' is not defined

未定义名称“MaxBug”