In this project, you will gain familiarity with Python's flexible "dictionary" data structure. You will build a movie database like IMDb.
You are to do this project individually.
Do Exercise 4.24 on page 139. Your makeDictionary
function should work with any two list (one of keys,
one of values), so long as they are of the same length as each other. For example, in addition to using the two lists
in the exercise, you could also have this session:
>>> d1 = makeDictionary(['a', 'b'], [1, 2]) >>> d2 = makeDictionary(['a', 'an', 'the'], ['indefinite', 'indefinite', 'definite']) >>> d1['a'] 1 >>> d2['a'] 'indefinite'
You are not required to handle lists of unequal lengths. Be sure the parameter and variable names within your function are descriptive and that you avoid excessive complexity.
PI
to be 3.1415, you can use PI
anywhere.
PI = 3.1415 # areaCircle : number -> number def areaCircle(radius): return PI * (radius ** 2)Now, define a global variable called
myIMDb
and set it an empty dictionary.The reason why this dictionary is initially empty is because your database will start out with no movies. As each movie is added, the myIMDb dictionary will gain one new pair. Here is what it would look like after one movie, Harry Potter and the Sorcerer's Stone, is added:
Notice that the myIMDb dictionary only has one pair in it, corresponding to the one movie. The key of that pair is the movie's title, a string, and the value is a whole separate dictionary. The second dictionary has one pair for each character in the movie; the key is the character's name, and the value is the actor's name. Notice that in the second dictionary, both the keys and values are strings.
addMovie
that adds a new pair into the dictionary referenced by the global variable myIMDb
. 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. For example, you will use the function as shown below.
>>> addMovie("Harry Potter and the Sorcerer's Stone", ['Harry Potter', 'Hermione Granger', 'Ron Weasley', 'Albus Dumbledore'], ['Daniel Radcliffe', 'Emma Watson', 'Rupert Grint', 'Richard Harris']) >>>Running the above Python code should register a new pair for the movie, leaving the
myIMDb
dictionary as shown in the previous diagram.
addMovie
three times to add three of your favorite movies to the global variable myIMDb
. The three uses of addMovie
should be included in the same myimdb.py file along with the functions you wrote. listMovies
that returns a list of titles of all the movies in the global variable myIMDb
.
>>> listMovies() ["Harry Potter and the Sorcerer's Stone"] >>>Use the function
listMovies
to double-check if the movies were added to the global variable myIMDb
correctly in Step 4.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.
>>> findActor("Harry Potter and the Sorcerer's Stone", 'Ron Weasley') 'Rupert Grint' >>> findActor("Harry Potter and the Sorcerer's Stone", 'Hairy Potter') No such character found '' >>> findActor('Hairy Potter', 'Hairy Potter') No such movie found ''
showCast
that takes a movie title and prints out the characters with corresponding actors/actresses from the given movie in an alphabetical order of characters. The columns must be aligned (20 characters (including the character's name) before the name of the actor/actress.) If the movie is not found, it prints out an error message.
>>> showCast("Harry Potter and the Sorcerer's Stone") Character Actor/Actress ---------------------------------------- Albus Dumbledore Richard Harris Harry Potter Daniel Radcliffe Hermione Granger Emma Watson Ron Weasley Rupert Grint >>> showCast('Hairy Potter') No such movie found
You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following file:
addMovie
.You will earn one point for each of the following accomplishments:
You wrote a correct contract and docstring for the function makeDictionary
.
Your makeDictionary
meets the stated expectations.
You defined the variable myIMDb
and initialized it to an empty dictionary.
You wrote a correct contract and docstring for the function addMovie
.
The function addMovie
correctly adds a new movie entry to the global variable myIMDb
.
The function addMovie
makes use of the function makeDictionary
.
You use the function addMovie
correctly at least 3 times.
You wrote a correct contract and docstring for the function listMovies
.
listMovies
returns a list of titles of all the movies in the global variable myIMDb
.
You wrote a correct contract and docstring for the function findActor
.
The function findActor
looks up the correct actor/actress for the given role in the given movie.
The function findActor
prints out the error messages and returns an empty string correctly.
You wrote a correct contract and docstring for the function showCast
.
The function showCast
prints the header of the table.
The function showCast
lists all the characters and actors/actresses from the right movie.
The function showCast
lists all the characters and actors/actresses in the alphabetical order.
The function showCast
formats the table correctly.
The function showCast
prints out the error message when the movie is not found.
You use descriptive names for parameters and variables used in your functions.
Your functions are not unnecessarily complex.