python 常见报错集锦

    科技2022-08-25  114

    # 1.未定义:NameError: name 'a' is not defined print(a) --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-70ed2873a890> in <module> 1 # 1.未定义 ----> 2 print(a) NameError: name 'a' is not defined # 2.类型不一致:TypeError: must(必须) be(是) str, not(不) int b = 'name' c = 123 b+c --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-1b1b33123c88> in <module> 2 b = 'name' 3 c = 123 ----> 4 b+c TypeError: must be str, not int # TypeError: unsupported operand type(s) for +: 'int' and 'str' c+b --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-baf87dc38fa6> in <module> ----> 1 c+b TypeError: unsupported operand type(s) for +: 'int' and 'str' # ValueError: invalid literal for int() with base 10: 'name' c+int(b) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-4-362903aa1601> in <module> ----> 1 c+int(b) ValueError: invalid literal for int() with base 10: 'name' # 语法错误:SyntaxError: invalid(无效的) character(字符) in(在) identifier(标识符) print123) File "<ipython-input-6-94af2698dffa>", line 2 print(123) ^ SyntaxError: invalid character in identifier # 漏符号:SyntaxError: invalid(无效的) syntax(语法) if 3>2 print(666) File "<ipython-input-7-75cad109330d>", line 2 if 3>2 ^ SyntaxError: invalid syntax # 太多符号导致遗漏:SyntaxError: unexpected EOF while parsing int(int(eval('123')) File "<ipython-input-8-11fff21863fc>", line 2 int(int(eval('123')) ^ SyntaxError: unexpected EOF while parsing # 4.缩进错误:IndentationError: unexpected(意想不到的) indent(缩进) if 2>1: print(2) print(3) File "<ipython-input-9-43763d7ec7c2>", line 4 print(3) ^ IndentationError: unexpected indent # 5.索引错误:IndexError: list index out of range(范围) list = [1,2,3] print(list[3]) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-10-16cd916f6e29> in <module> 1 # 5.索引错误 2 list = [1,2,3] ----> 3 print(list[3]) IndexError: list index out of range # 6.NoteType错误,一般发生在连写的时候 dict2 = {'a':'12345','B':'45678'} dict2.get('a')[-1] dict2.get('b')[-1] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-d6fb3babac46> in <module> 2 dict2 = {'a':'12345','B':'45678'} 3 dict2.get('a')[-1] ----> 4 dict2.get('b')[-1] TypeError: 'NoneType' object is not subscriptable dict2 = {'a':'12345','B':'45678'} print(dict2.get('b').find_all('img')) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-2df3f5fc235b> in <module> 1 dict2 = {'a':'12345','B':'45678'} ----> 2 print(dict2.get('b').find_all('img')) AttributeError: 'NoneType' object has no attribute 'find_all'
    Processed: 0.008, SQL: 9