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.沒有自己代碼行的報錯,看源碼進行分析,這個是最難的,一點點排查把