Python常见错误整理

IndexError
IndexError: tuple index out of range
    tuple类型索引出界了
    print((1,2)[3])
IndexError: pop from empty list
    从空的list中删除
    print([].pop(1))



ImportError
ImportError: No module named 'sy'
    没有模块名字叫sy
    import sy



KeyError
KeyError: 4
    没有4这个键
    print({1:11, 2:22, 3:33}[4])
KeyError: 'popitem(): dictionary is empty'
    字典是空的
    {}.popitem()



NameError
NameError: name 'k' is not defined
    变量k没有定义
    print(k)



SyntaxError
SyntaxError: invalid syntax
    不合规范的语法
    for i in range(0,10)):



TypeError
TypeError: 'int' object is not subscript able
    创建包含一个元素的元组,要在该元素后面添加一个逗号。
    print((1,)[0])
    print((1)[0])
TypeError: 'tuple' object does not support item assignment
    该类型不支持此操作(改值)
    (1,2)[0]=0
TypeError: not all arguments converted during string formatting
    参数多了
    print("%d" % (1,2))
TypeError: not enough arguments for format string
    参数少了
    print("%d %d" % 1)
TypeError: pop expected at least 1 arguments, got 0
    参数少了(至少一个,一个也没有得到)
    print(dt.pop())
TypeError: insert() takes exactly 2 arguments (1 given)
    要两个参数只给了一个
    print([].insert(6))
TypeError: unhashable type: 'list'
    list类型是不能做为键(不可哈希)的类型
    print({[1,]:11, 2:22}[[1,]])
TypeError: 'myTest' is an invalid keyword argument for this function
    myTest不是print方法的默认参数,不能这样写
    print(myTest=3)



UnboundLocalError
UnboundLocalError: local variable 'i' referenced before assignment
    引用局部变量i前应该定义
    i*=2



ValueError
ValueError: range() arg 3 must not be zero
    range方法的第三个参数不可为0
    for i in range(1, 3, 0):



ZeroDivisionError
    ZeroDivisionError: division by zero
    除数不能为0
    print(5/0)


BaseException
    所有异常的基类
SystemExit
    解释器请求退出
KeyboardInterrupt
    用户中断执行(通常是输入^C)
Exception
    常规错误的基类
StopIteration
    迭代器没有更多的值
GeneratorExit
    生成器(generator)发生异常来通知退出
SystemExit
    Python 解释器请求退出
StandardError
    所有的内建标准异常的基类
ArithmeticError
    所有数值计算错误的基类
FloatingPointError
    浮点计算错误
OverflowError
    数值运算超出最大限制
ZeroDivisionError
    除(或取模)零 (所有数据类型)
AssertionError
    断言语句失败
AttributeError
    对象没有这个属性
EOFError
    没有内建输入,到达EOF 标记
EnvironmentError
    操作系统错误的基类
IOError
    输入/输出操作失败
OSError
    操作系统错误
WindowsError
    系统调用失败
ImportError
    导入模块/对象失败
KeyboardInterrupt
    用户中断执行(通常是输入^C)
LookupError
    无效数据查询的基类
IndexError
    序列中没有没有此索引(index)
KeyError
    映射中没有这个键
MemoryError
    内存溢出错误(对于Python 解释器不是致命的)
NameError
    未声明/初始化对象 (没有属性)
UnboundLocalError
    访问未初始化的本地变量
ReferenceError
    弱引用(Weak reference)试图访问已经垃圾回收了的对象
RuntimeError
    一般的运行时错误
NotImplementedError
    尚未实现的方法
SyntaxError
    Python 语法错误
IndentationError
    缩进错误
TabError
    Tab 和空格混用
SystemError
    一般的解释器系统错误
TypeError
    对类型无效的操作
ValueError
    传入无效的参数
UnicodeError
    Unicode 相关的错误
UnicodeDecodeError
    Unicode 解码时的错误
UnicodeEncodeError
    Unicode 编码时错误
UnicodeTranslateError
    Unicode 转换时错误
Warning
    警告的基类
DeprecationWarning
    关于被弃用的特征的警告
FutureWarning
    关于构造将来语义会有改变的警告
OverflowWarning
    旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning
    关于特性将会被废弃的警告
RuntimeWarning
    可疑的运行时行为(runtime behavior)的警告
SyntaxWarning
    可疑的语法的警告
UserWarning
    用户代码生成的警告

猜你喜欢

转载自blog.csdn.net/weixin_42404145/article/details/80744639