# 第一种:
str = 'Hello World'
# print(str[20])
# IndexError: string index out of range
# 索引错误:字符串超出范围
# 解决方法:索引小于字符串的长度
print(str[4]) # o
# 第二种:
list = ['a','b','c','d','e']
# print(list[20])
# IndexError: list index out of range
# 索引错误:列表索引超出了范围
# 解决办法:索引小于字符串的长度
print(list[3]) # d
# 第三种:
str = 'Hello World'
# print(str.index('a'))
# ValueError: substring not found
# 值错误:子字符串未找到
# 解决办法:查找包含在字符串里面的字符串
print(str.index('H')) # 0
# 第四种:
# count = 1
# if count == 20:
# return
# SyntaxError: 'return' outside function
#语法错误:return不能在方法外使用
# 解决方法:将return放在方法体中
class A:
name = 'return'
def error(self):
count = 1
if count == 1:
print('这是return的用法')
return
a = A
a.error(a) # 这是return的用法
# 第五种:
name = '小兰'
# if name == '小兰'
# SyntaxError: invalid syntax
# 语法错误:非法的语法
# 解决办法:看报错信息在第几行,从这一行往上找错误
if name == '小兰':
print(name) # 小兰
# 第六种:
name = '小明'
# age = 20
# print('我的名字是' + name + '我的年龄是' + age)
# TypeError: must be str, not int
# 类型错误:必须为字符串格式,不能是数字格式
# 解决方法:使用+拼接的时候 必须使用字符串 或者将数字转化成字符串
age = '20'
print('我的名字是' + name + ',我的年龄是' + age)
# 我的名字是小明,我的年龄是20
# 第七种:
dic1 = {
'name' : '小明',
'age' : 20,
'friend' : '小兰'
}
print(dic1) # {'name': '小明', 'age': 20, 'friend': '小兰'}
# dic1.pop()
# TypeError: pop expected at least 1 arguments, got 0
# 类型错误:pop方法希望得到至少一个参数,但是现在参数为0
# 解决办法:给pop方法添加字典中有的参数
dic1.pop('age')
print(dic1) # {'name': '小明', 'friend': '小兰'}
# 第八种:
dic = {
'name' : '小明',
'age' : 20
}
# print(dic['sex'])
# KeyError: 'sex'
# key键错误:没有指定的键值'sex'
# 解决办法:创建这个键,并给这个键赋值
dic['sex'] = '男'
print(dic['sex']) # 男
# 第九种:
str = ('a','b','c')
print(str)
# str.remove(1)
# AttributeError: 'tuple' object has no attribute 'remove'
# 属性错误:元组对象没有属性'remove'
# 第10种:
# for x in range(10):
# if x == 3:
# print(x)
# IndentationError: expected an indented block
# 缩进错误:此处只需要缩进
# 解决办法:tab自动缩进
for x in range(10):
if x == 3:
print(x)
#第11种:
#selenium.common.exceptions.NoSuchElementException:
# Message: Unable to locate element: [id="kw"]
# 错误原因:代码执行速度很快,但是浏览器响应很慢,代码执行到这儿的时候
# 浏览器里面的元素可能还没有加载完 所以报错找不到指定的元素