1. TypeError: must be str, not int
类型错误:必须是一个字符串 不能是数字
错误演示:
name = '熊晨'
age = 20
print('我的名字叫'+name+',我的年龄是'+age)
解决方案:使用‘+’拼接的时候,必须使用字符串,或者将数字转化为字符串
正确演示:
name = '熊晨'
age = '20'
print('我的名字叫'+name+',我的年龄是'+age)
2.IndentationError: unindent does not match any outer indentation level
缩进错误:未知缩进不匹配任何缩进等级
错误演示:
for index in range(10):
if name == '小王':
print('hello')
else:
print('nothing')
解决方案:tab自动缩进
正确演示:
for index in range(10):
if name == '小王':
print('hello')
else:
print('nothing')
3.IndexError: string index out of range
索引错误:字符串超出了范围
错误演示:
str = 'good idea'
print(str[20])
解决方案:查看字符串的长度,索引要小于长度
正确演示:
str = 'good idea'
print(str[2])
4.SyntaxError: invalid syntax
语法错误:非法的语法
错误演示:
name = '小陈'
if name = '小陈'
print('hello')
解决方案:看报错的信息在第几行,在这一行上找找错误
正确演示:
name = '小陈'
if name == '小陈':
print('hello')
5.ValueError: substring not found
值错误:子字符串未找到
错误演示:
str = 'hello world'
result = str.index('c')
print(result)
解决方案:输入字符串内的字符
正确演示:
str = 'hello world'
result = str.index('l')
print(result)
6.AttributeError: 'tuple' object has no attribute 'remove'
属性错误:元组对象没有属性‘remove’
错误演示:
tp1 = ((),[],{},1,2,3,'a','b','c',3.14 ,True)
tp1.remove(1)
print(tp1)
解决方案:没有此种属性那就不要用
7.KeyError: 'fond'
key键错误:没有指定的键‘fond’
错误演示:
dic1 = {
'name': '小许',
'age' : 17 ,
'friend':['嘻嘻','嚷嚷','慌慌','张张','欣欣','向荣']
}
print(dic1['fond'])
解决方案:给字典中指定的键赋值 ,如果有这个键 则重新修改这个键对应的值,如果没有这个键 则创建这个键 并且设置对应的值
正确演示:
dic1 = {
'name': '小许',
'age' : 17 ,
'friend':['嘻嘻','嚷嚷','慌慌','张张','欣欣','向荣']
}
dic1['fond']='学习Python'
print(dicl)
8.TypeError: pop expected at least 1 arguments, got 0
类型错误:pop方法希望得到至少一个参数,但是现在参数为0
错误演示:
dic1 = {
'name': '小许',
'age' : 17 ,
'friend':['嘻嘻','嚷嚷','慌慌','张张','欣欣','向荣']
}
dic1.pop()
print(dic1)
解决方案:
正确演示:
dic1 = {
'name': '小许',
'age' : 17 ,
'friend':['嘻嘻','嚷嚷','慌慌','张张','欣欣','向荣']
}
dic1.pop('name')
print(dic1)