在敲代码的过程当中老是遇到各类各样的错误。为了遇到认识的错误知道怎么处理,所以总结错误类型以下:html
1. IndexError : string index out of range python
索引错误:字符串索引超出了范围ide
解决办法:查看字符串的长度,索引要小于长度编码
content ='hello world' print(content[11])
IndexError : list index out of ragespa
索引错误:列表索引超出了范围code
解决办法:查看列表的长度,索引要小于长度htm
list1 =['outMan','小李子','诺兰','皮克斯'] print(list1[5])
2. SyntaxError:'return' outside function对象
语法错误:return 不能在方法之外使用索引
解决办法:不用return或者将return放在方法中rem
while True: count +=1 if count ==20: return
SyntaxError: invalid syntax
语法错误:非法的语法
解决办法:看报错信息在第几行,从这一行上找错误
name = '小王' if name =='小王' print('Hello')
3. TypeError: must be str,not int
类型错误 : 必须使用字符串类型,不能用数值类型
解决办法:使用+拼接的时候,必须使用字符串,或者将数字转化成字符串
name ="小王" age =16 print('个人名字是'+name+',个人年龄是'+age)
TypeError : pop expected at least 1 arguments,got 0
arguments 参数 expected 指望 at least 至少
类型错误:pop方法但愿获得至少一个参数,可是如今参数为0
dic ={'name' :'张三'} dic.pop() print(dic)
4.IndentationError : unindent does not match any outer indentation level
缩进错误:未知缩进 不匹配任何缩进等级
解决办法 :tab自动缩进
name ='小王' if name =='小王': print('hello') else: print('nothing')
5. KeyError : ‘fond’
Key键错误(关键字错误):没有指定的键值“fond”
解决办法:添加相应的键值对或者寻找存在的键值
dic1 ={'name':'张三'} print(dic1['fond'])
6. ValueError : substring not found
值错误:子字符串未找到
解决办法:找已有的子字符串
content ='hello world' result = content.index('x') print(result)
7. AttributeError:'tuple' object has no attribute 'remove'
attribute 属性 object 对象
属性错误: 元组对象没有属性‘remove’
tp1 =((),[],{},1,2,3,'a','b','c',3.14,True) tp1.remove(1) print(tp1)
8. UnicodeEncodeError:'locale' codec can't encode character
编码错误:本地文件不能对指定位置的字符进行编码
date = date.strftime('%Y' 年%m 月 %d day) print(date)
未完待续: