我有一个这样的问题
为函数 findActor 编写合约、文档字符串和实现,该函数接受电影标题和角色名称并返回在给定电影中扮演给定角色的演员/女演员。如果找不到给定的电影或给定的角色,它会打印一条错误消息并返回一个空字符串
我已经完成了以下有助于执行此操作的功能。而myIMDb是一个全局字典,设置为空dic启动
def addMovie (title, charList, actList):
"""The function addMovie takes a title of the movie, a list of characters,
and a list of actors. (The order of characters and actors match one
another.) The function addMovie adds a pair to myIMDb. The key is the title
of the movie while the value is a dictionary that matches characters to
actors"""
dict2 = {}
for i in range (0, len(charList)):
dict2 [charList[i]] = actList[i]
myIMDb[len(myIMDb)] = {title: dict2}
return myIMDb
def listMovies():
"""returns a list of titles of all the movies in the global variable myIMDb"""
titles = []
for i in range (len(myIMDb)):
titles.append((list(myIMDb[i].keys())))
return titles
这就是我遇到问题的地方。当我想编写 findActor 函数时,我什么也得不到。我还没有完成这个功能,但我认为我做了一些根本错误的事情。我觉得我走错了路,我写得越多,我就越迷失。这就是我想要的。任何有关如何纠正这艘正在下沉的船的建议将不胜感激。
def findActor(title, name):
myIMDb = {}
for i in range (len(myIMDb)):
if title == myIMDb[i].keys():
if name == myIMDb[i].get(name):
return myIMDb[i].get(name)
else:
return "Error: No Movie found"