复制代码
1
2
3
4# 1.未定义:NameError: name 'a' is not defined print(a)
复制代码
1
2
3
4
5
6
7
8
9--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-70ed2873a890> in <module> 1 # 1.未定义 ----> 2 print(a) NameError: name 'a' is not defined
复制代码
1
2
3
4
5
6# 2.类型不一致:TypeError: must(必须) be(是) str, not(不) int b = 'name' c = 123 b+c
复制代码
1
2
3
4
5
6
7
8
9--------------------------------------------------------------------------- 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
复制代码
1
2
3
4# TypeError: unsupported operand type(s) for +: 'int' and 'str' c+b
复制代码
1
2
3
4
5
6
7--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-baf87dc38fa6> in <module> ----> 1 c+b TypeError: unsupported operand type(s) for +: 'int' and 'str'
复制代码
1
2
3
4# ValueError: invalid literal for int() with base 10: 'name' c+int(b)
复制代码
1
2
3
4
5
6
7--------------------------------------------------------------------------- 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'
复制代码
1
2
3
4# 语法错误:SyntaxError: invalid(无效的) character(字符) in(在) identifier(标识符) print(123)
复制代码
1
2
3
4
5
6File "<ipython-input-6-94af2698dffa>", line 2 print(123) ^ SyntaxError: invalid character in identifier
复制代码
1
2
3
4
5# 漏符号:SyntaxError: invalid(无效的) syntax(语法) if 3>2 print(666)
复制代码
1
2
3
4
5
6File "<ipython-input-7-75cad109330d>", line 2 if 3>2 ^ SyntaxError: invalid syntax
复制代码
1
2
3
4# 太多符号导致遗漏:SyntaxError: unexpected EOF while parsing int(int(eval('123'))
复制代码
1
2
3
4
5
6File "<ipython-input-8-11fff21863fc>", line 2 int(int(eval('123')) ^ SyntaxError: unexpected EOF while parsing
复制代码
1
2
3
4
5
6# 4.缩进错误:IndentationError: unexpected(意想不到的) indent(缩进) if 2>1: print(2) print(3)
复制代码
1
2
3
4
5
6File "<ipython-input-9-43763d7ec7c2>", line 4 print(3) ^ IndentationError: unexpected indent
复制代码
1
2
3
4
5# 5.索引错误:IndexError: list index out of range(范围) list = [1,2,3] print(list[3])
复制代码
1
2
3
4
5
6
7
8
9
10--------------------------------------------------------------------------- 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
复制代码
1
2
3
4
5
6# 6.NoteType错误,一般发生在连写的时候 dict2 = {'a':'12345','B':'45678'} dict2.get('a')[-1] dict2.get('b')[-1]
复制代码
1
2
3
4
5
6
7
8
9--------------------------------------------------------------------------- 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
复制代码
1
2
3dict2 = {'a':'12345','B':'45678'} print(dict2.get('b').find_all('img'))
复制代码
1
2
3
4
5
6
7
8--------------------------------------------------------------------------- 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'
最后
以上就是和谐萝莉最近收集整理的关于python 常见报错集锦的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复