马士兵Python学习笔记_P100_99.Bug的常见类型-思路不清导致的问题
一、Bug的由来及分类
二、代码示例
print("------------------------------- (1) 注释掉报错的代码,多用print输出------------------------------------")
# 豆瓣电影Top250排行,使用列表存储电影信息,要求输入名字在屏幕上显示XXX出演了哪部电影
lst = [{"rating": [9.7, 50], "id": "1292052", "type": ["犯罪", "剧情"], "title": "肖申克的救赎", "actors": ["蒂姆.罗宾斯", "摩根.弗里曼"]},
{"rating": [9.6, 50], "id": "1291546", "type": ["剧情", "爱情", "同性"], "title": "霸王别姬", "actors": ["张国荣", "张丰毅", "巩俐", "葛优"]},
{"rating": [9.6, 50], "id": "1296141", "type": ["剧情", "犯罪", "悬疑"], "title": "控方证人", "actors": ["泰隆.鲍华", "玛琳.黛德丽"]}
]
name = input("请输入你要查询的演员名字:")
for item in lst: # 遍历列表 --> {}, item是一个又一个字典
act_list = item["actors"]
# print(act_list)
for actor in act_list:
if name in actor:
print(name + "出演了:" + item["title"])
# for movie in item: # 遍历字典,得到的 movie 是一个字典中的 key
# print(movie)
# print("----------------")
# actors = movie["actors"] # 报错:TypeError: string indices must be integers, not 'str'
# print(actors)
# if name in actors:
# print(name + "出演了:" + movie)
运行结果:
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnpython\马士兵Python\第11章_全民来找茬\P100_99.Bug的常见类型-思路不清导致的部题.py
------------------------------- (1) 注释掉报错的代码,多用print输出------------------------------------
请输入你要查询的演员名字:张国荣
张国荣出演了:霸王别姬
Process finished with exit code 0