- 常见bug盘点
- 思路不清导致的bug: 使用print(),注释剩余代码,一步步调试
- python的异常处理机制
try:… except xxError:…
try except else
try except else finally
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3c4ff5a00baff217799c32603d4efadc.jpeg#pic_center)
- 异常处理模块:手动打印异常信息 traceback.print_exe()
lst = [1,2,3,4]
dict = {'name':'NANA', 'score':99}
'''
lst = [{'Band':'Black Stone', 'Members':['NANA','Tai','Zhen','Shen'], 'Score':11},
{'Band':'Trap Nest', 'Members':['Layla','Lynn','Qiao','Zhi'], 'Score':22},
{'Band':'Hedgehog', 'Members':['ShiLu','ZiJian','YiFan'], 'Score':33}]
# 查询成员所在乐队
name = input('要查询的人是:') # NANA
for item in lst: # 遍历列表,get三个字典
name_lst = item['Members'] # get各自的成员列表
for i in name_lst:
if name in i:
print(name,'属于',item['Band'],'乐队') # NANA 属于 Black Stone 乐队
# 查询某乐队的得分
bname = input('要查询的乐队是:') # Hedgehog
for item in lst:
if bname == item['Band']:
print(bname+'的分数是'+str(item['Score'])) # Hedgehog的分数是33
print(bname,'的分数是',item['Score']) # Hedgehog 的分数是 33
'''
'''
a = int(input('请输入第一个整数:'))
b = int(input('请输入第二个整数:'))
result = a/b
print(result)
# 可能问题:输入的a手滑输入键盘字符,输入的b不小心为0,so改进
# 捕获异常顺序:先子类,后父类(后议)
'''
'''
try:
a = int(input('请输入第一个整数:'))
b = int(input('请输入第二个整数:'))
result = a / b
print('结果为',result)
except ValueError:
print('第一个数不能为字符!')
except ZeroDivisionError:
print('除数不能为0!')
print('程序结束')
'''
'''
try:
a = int(input('请输入第一个整数:'))
b = int(input('请输入第二个整数:'))
result = a / b
except BaseException as e: # 通用错误BaseException;A as B 给A起别名为B
print('出错啦!',e)
else:
print('结果为',result)
print('程序结束')
'''
'''
try:
a = int(input('请输入第一个整数:'))
b = int(input('请输入第二个整数:'))
result = a / b
except BaseException as e: # 通用错误BaseException;A as B 给A起别名为B
print('出错啦!',e)
else:
print('结果为',result)
finally:
print('程序结束')
'''
'''
import traceback
try:
print('------------注意这条线的位置(随机,多线程,后议)-------------')
a = 3/0
except:
traceback.print_exe()
'''