Python various error reports (random update...)

An exception is an event that will occur during program execution and affect the normal execution of the program. Under normal circumstances, an exception occurs when Python cannot handle the program normally. The exception is a Python object that represents an error.

Exception name description
ArithmeticError The base class for all numerical calculation errors, common in试图访问一个不存在的对象
AssertionError The assertion statement fails. This exception is thrown when the assertion statement does not meet the conditions.
AttributeError If the object does not have this attribute, an attribute error will be raised when the attribute reference and assignment fail. If it is the called standard module or third-party module, confirm whether there is a custom file or directory with the same name
BaseException Base class for all exceptions
DeprecationWarning Warning about deprecated features
EnvironmentError Base class for operating system errors
EOFError No built-in input, reach the EOF mark
Exception Base class for general errors
FloatingPointError Floating point calculation error
FutureWarning Warning about structural changes in semantics in the future
GeneratorExit The generator (generator) has an exception to notify the exit
ImportError Failed to import module/object
IndentationError Indentation error
IndexError There is no such index in the sequence, the index is wrong, the used index does not exist, often the index exceeds the range of the sequence, there is no such index in the sequence
IOError Input/output operation failed: the file does not exist, an IOError error is encountered when the file is written, or a permission problem is caused
KeyboardInterrupt User interrupt execution (usually input ^C)
KeyError There is no such key in the mapping. When reading the key and value in the dictionary, if the key does not exist, a KeyError will be triggered.
LookupError Base class for invalid data query
MemoryError Out of memory error (not fatal to the Python interpreter)
NameError The object is not declared/initialized (no attributes), the name of the variable you are trying to access does not exist
NotImplementedError Method not yet implemented
OSError Operating system error
OverflowError Numerical calculation exceeds the maximum limit
OverflowWarning Old warning about automatic promotion to long
PendingDeprecationWarning Warning about features that will be obsolete
ReferenceError Weak reference attempts to access objects that have been garbage collected
RuntimeError General runtime errors
RuntimeWarning Suspicious runtime behavior warning
StandardError Base class for all built-in standard exceptions
StopIteration Iterator has no more values
SyntaxError Python syntax error
SyntaxWarning Suspicious syntax warning
SystemError General interpreter system error, syntax error, code format error
SystemExit Interpreter request to exit
TabError Tab and space mixed
TypeError For operations that are invalid for the type, an error occurs when the object is used to represent the type of the value that is not the expected type
UnboundLocalError Access uninitialized local variables
UnicodeDecodeError Unicode decoding error
UnicodeEncodeError Unicode encoding error
UnicodeError Unicode related errors
UnicodeTranslateError Error during Unicode conversion
UserWarning Warning generated by user code
ValueError Invalid parameter
Warning Base class for warnings
WindowsError System call failed
ZeroDivisionError Divide (or modulo) zero (all data types)

一、TypeError:类型错误,对象用来表示值的类型非预期类型时发生的错误

  • 错误例子:

           age=18

           print(‘我的年龄是’+age)

  • 报错信息:TypeError: can only concatenate str (not "int") to str

        (这是类型错误,提示必须是一个字符串,不能是数字。)

  • 解决方法:在使用“+”做拼接的时候,必须使用字符串,或者把数字转化成字符串。
  • 正确代码:

          age=18

          print(‘我的年龄是’+str(age))

二、AttributeError:属性错误,特性引用和赋值失败时会引发属性错误

  • 错误例子:

          tp=('1','3','5')

          tp.append('7')

          print(tp)

  • 报错信息:AttributeError: 'tuple' object has no attribute 'append'

        提示:  属性错误:元组对象没有“append”的属性,简单来说元组是不可变对象

  • 解决方法:把元组换成列表。

三、NameError:试图访问的变量名不存在。

  • 举个简单的例子,只运行一行print(a),会报错:NameError: name 'a' is not defined。
  • 简单来说,就是变量a没有定义,解决方法是在前面给a赋值,比如加上一行a=''。
  • 还有一种需要小心处理的是:变量或者函数名拼写错误。如:

          name= '卡西'

         print('My name is ' + nane)

        报错信息和上面一样:NameError: name 'nane' is not defined。这类错误只要根据报错信息,细心检查下拼写,就能很快解决~

四、SyntaxError:语法错误,代码形式错误

        

  • 报错信息为:SyntaxError: invalid syntax

          提示是:语法错误,非法的语法。

  • 解决方法:

         当报错的时候,要注意回到错误信息的那一行,然后从下往上,慢慢查找,此处这个程序就是因为if语法忘了在判断语句后面加“:”,所以导致的错误。

        另外,初学者写代码时要注意使用英文符号,这是大家经常犯的错误

         

五、KeyError:在读取字典中的key和value时,如果key不存在,就会触发KeyError错误。

  • 举个小栗子:

          dict={'a':1,'b':2,'c':3}

          print(dict['d'])

         报错信息为:KeyError: 'd'。

  • 第一种解决方法,首先测试key是否存在,然后才进行下一步操作,如:

          

 

六、IndexError:索引错误,使用的索引不存在,常索引超出序列范围,序列中没有此索引(index)

        

 

  • 报错信息:IndexError: list index out of range

          提示:索引错误,列表索引超出了范围。

  • 解决办法:

         回头看列表的长度,索引是要小于列表的长度的。上面的列表长度是5,而索引却要打印第七个,所以是超出了列表的长度

七、IndentationError:缩进错误

        

 

  • 报错:IndentationError: expected an indented block

          原因:缩进有误,Python的缩进非常严格,行首多个空格,少个空格都会报错。

          由于不熟悉python编码规则,不易查找,所以会成为新手常犯的一个错误。像def,class,if,for,while等代码块都需要缩进。

八、TabError: Tab 和空格混用

       相邻的两句的缩进需要统一使用tab或统一使用空格,不能混用。否则就会出现以下报错:TabError: inconsistent use of tabs and spaces in indentation。

  • tab键本质上是制表符,而不是缩进符,不同的文本编辑器中制表符代表的空格宽度不一,如果代码需要跨平台或跨编辑器读写,建议不要使用制表符(tab键)。

        心得:inconsistent use of tabs and spaces in indentation

        错误处理方法:

       把报错的行光标放到第一个字母前,backspace直接删除到和上一行接起来,然后按回车就行了。

Guess you like

Origin blog.csdn.net/weixin_52071682/article/details/113869872