简体   繁体   中英

Python: Function that searches through a dictionary in a dictionary

I have a question that goes like this

Write the contract, docstring and implementation for a function findActor that takes a movie title and a character's name and returns the actor/actress that played the given character in the given movie. If the given movie or the given character is not found, it prints out an error message and returns an empty string

I have already done the following functions that will be of assistance for doing this. And myIMDb is a global dictionary, set to an empty dic to start

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

Here's where I'm having problems. When I want to write the findActor function I'm getting nothing to return. I'm not finished with the function but I'm thinking that i've done something fundamentally wrong. I feel like I'm going down the wrong road and I'm getting more and more lost the more I write. Here's what I have so for. Any suggestions for how to right this sinking ship would be appreciated.

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"

You need to populate your myIMDB dictionary in findActor before you use it.

In addition, I'd suggest mapping myIMDB directly from the title of the move to characters. In other words, instead of doing myIMDb[len(myIMDb)] = {title: dict2} in your addMoive , you should just do myIMDb[title] = dict2 .

This way, when you need to look up the title and character, you can simply do:

def findActor(title, name):
    if title in myIMDb:
        if name in myIMDb[title]:
            return myIMDb[title][name]
    return "Error: No movie found"

The first thing to learn, programming in any language, is to reduce your task to sub-tasks. Here, why not start first with creating just a dictionary of roles and actors for a single movie. If you can't do that, then you won't be able to complete the full project.

After you work on that, maybe everything else will fall into place.

Warning: in the real world, occasionally more than one actor may play a role - for example - a role in which a child matures into adulthood. But that likely is not in your spec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM