python 常见报错

python 常见报错

IndentationError: 缩进错误

IndentationError: expected an indented block
应为缩进快 
必须统一使用tab 不能空格和tab混用

AttributeError: 属性错误

AttributeError: 'int' object has no attribute 'replace'
“int”对象没有属性“replace”
AttributeError: module 'datetime' has no attribute 'now'
包引用错误模块“datetime”没有“now”属性
先检查是否拼写错误,再检查有没有安装此包

NameError:名称错误

NameError: name 'lst' is not defined
未定义名称 
检查引号中的内容单词是否错误

SyntaxError:语法错误

SyntaxError: invalid syntax
无效语法 
先检查单词是否写错 标点是否错误 
SyntaxError: no binding for nonlocal 'a' found
未找到非本地“a”的绑定
SyntaxError: can't assign to literal
无法分配给文本
SyntaxError: unexpected EOF while parsing
分析时出现意外的EOF

TypeError: 类型错误

TypeError: 'type' object is not subscriptable
对象不可订阅
TypeError:can only concatenate str (not "int") to str
自能将字符串和字符串拼接,不能拼接int类型
TypeError: 'builtin _function _or _method ' object is not iterable
对象不可重复
TypeError: 'str' object cannot be interpreted as an integer
对象不能解释为整数
TypeError: 'str' object is not callable
不可以被系统调用
TypeError: 'str' object does not support item assignment
对象不支持项分配
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'float'
不支持-:“builtin_function_or_method”和“float”的操作数类型
TypeError: argument of type 'int' is not iterable
类型“int”的参数不可迭代
TypeError: string indices must be integers
字符串索引必须是整数
TypeError: list expected at most 1 arguments, got 2
列表最多需要1个参数,得到2个
TypeError: sequence item 2: expected str instance, int found
序列项2:应为str实例,找到int
TypeError: can only concatenate str (not "list") to str
只能将str(不是“list”)连接到str
TypeError: unhashable type: 'list'
不可显示的类型:list
TypeError: extend() takes no keyword arguments
不接受关键字参数
TypeError: 'int' object is not callable
int对象不能被调用
TypeError: Can't instantiate abstract class wechatpay with abstract methods pay
无法用抽象方法pay实例化抽象类微信支付

KeyError: 键错误

KeyError: 'l'
键'l'错误

IndexError: 索引错误

IndexError: tuple index out of range
元组索引超出范围
IndexError: string index out of range
字符串索引超出范围

ValueError: 值错误

ValueError: substring not found
找不到字符串
ValueError: list.remove(x): x not in list
不在列表内
ValueError: attempt to assign sequence of size 1 to extended slice of size 2
尝试将大小为1的序列分配给大小为2的扩展切片
ValueError:invalid literal for int() with base 10: '3.8'
浮点型字符串无法强制转换成int型

RecursionError: 递归错误

RecursionError: maximum recursion depth exceeded while calling a Python object
调用python对象时超过了最大递归深度

json报错

json.decoder.JSONDecodeError: Extra data: line 1 column 13 (char 12)
额外数据:第1行第13列(char 12)

未绑定的局部错误

UnboundLocalError: local variable 'a' referenced before assignment
在赋值之前引用了局部变量“a”

网络编程错误

OSError: [Errno 48] Address already in use
地址已在使用中
关闭正在运行的同地址的即可
argument of type 'int' is not iterableConnectionRefusedError: [Errno 61] Connection refused
类型“int”的参数不是ITerableConnectionReutedError:[errno 61]连接被拒绝
没有启动服务器,启动服务器即可

遇到错误不要慌张

1.看哪一行有报错,一般都是最后一行;根据提示进行修改,一般是引用错误啊,语法错误啊什么的
2.实在找不到就print,一行行打印结果排查
3.没有自己代码行的报错,看源码进行分析,这个是最难的,一点点排查把

原文地址:https://www.cnblogs.com/zhangshan33/p/11136510.html