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 of the textbook. 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
written in the textbook, you could also have this session:
>>> d1 = makeDictionary(['a', 'b'], [1, 2]) >>> d2 = makeDictionary(['a', 'an', 'the'], ['indefinite', 'indefinite', 'definite']) >>> d3 = makeDictionary([], []) >>> d1['a'] 1 >>> d2['a'] 'indefinite' >>> d3 {} >>>
Note: You do not need to handle lists of unequal lengths. You should look back to your Moodle homework Week 7 Monday.
You must ensure that your makeDictionary
function does not change the input list values.
Your function should take advantage of the power of the dictionary structure.
Test your procedure and fix it if it doesn't work. 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)
myIMDb
(Note the lower case b
at the end of the name!) and set it to 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, currently, the myIMDb dictionary only has one pair in it, corresponding to the one movie. The key of that pair is the movie's title (in the form of a Python 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']) >>> >>> myIMDb["Harry Potter and the Sorcerer's Stone"] {'Albus Dumbledore': 'Richard Harris', 'Harry Potter': 'Daniel Radcliffe', 'Hermione Granger': 'Emma Watson', 'Ron Weasley': 'Rupert Grint'}
Running the above Python code should register a new pair for the movie, leaving the myIMDb
dictionary as shown in the previous diagram.
You must ensure that your addMovie
function does not change the input list values.
Your function addMovie
should return nothing.
Your function addMovie
should print an error message if there is already a movie with the same title. The error message should print exactly as follows.
>>> addMovie("Harry Potter and the Sorcerer's Stone", ['Hermione Granger', 'Ron Weasley'], ['Emma Watson', 'Rupert Grint']) Movie not added. Harry Potter and the Sorcerer's Stone is an existing key. >>>
Your function should take advantage of the power of the dictionary structure. In particular, you should not have any loop in the body of this function.
Test your procedure and fix it if it doesn't work.
addMovie
three times to add three of your favorite movies to the global variable myIMDb
.
The three uses of addMovie
should be included at the bottom of your project5.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"] >>>
This should be a short, simple function. You should not need to include any loop in this function (although it's OK to have a very simple loop if you wish). Ask your professor or TA if you're not sure how to make this uncomplicated.
Use the function listMovies
to double-check if the movies were added correctly to the global variable myIMDb
correctly in the previous task using the addMovie
function.
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.
>>> findActor("Harry Potter and the Sorcerer's Stone", 'Ron Weasley') 'Rupert Grint' >>> findActor("Harry Potter and the Sorcerer's Stone", 'Hermione Granger') 'Emma Watson'
If the given movie title is not a key in the myIMDb
dictionary,
your function should return an empty string as well as
printing an error message. The error message should be exactly as shown below.
>>> findActor('Hairy Potter', 'Ron Weasley') # Movie Hairy Potter is not in myIMDb Movie Hairy Potter is not found '' >>> findActor('Hairy Potter', 'Ronnie Weasel') # Movie Hairy Potter is not in myIMDb Movie Hairy Potter is not found ''
If the given movie title exists in the myIMDb
dictionary but the
given character is not a key in the dictionary corresponding to the given movie title, your findActor
should return an empty string as well as
printing an error message. The error message should be exactly as shown below.
>>> findActor("Harry Potter and the Sorcerer's Stone", 'Ronnie Weasel') # Character Ronnie Weasel is not a character in the given movie Character Ronnie Weasel is not found ''
Your function should take advantage of the power of the dictionary structure. In particular, you should not have any loop in the body of this function.
Write the contract, docstring and implementation for a function showCast
that takes a movie title and prints out the characters with corresponding actors/actresses from the given movie (in an alphabetical order of movie characters). The columns must be aligned such that there are 20 characters (including the movie character's name) before the name of the actor/actress.
This table should include a header, where underneath the header is 40 copies of the minus sign "-"
.
>>> 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
When testing your work, we will not use any movie character whose name consists of more than 19 characters. So don't worry about movie characters whose names take up 20 or more characters.
Hint:
You may want to use one of the string methods from Table 3.2 to left justify your first column so that there 20 characters (of movie character plus spaces) are printed.
This table is supposed to be very similar to the table you created for the Moodle homework Week 7 Monday.
This function should return nothing.
If the movie is not found, it prints out an error message.
>>> showCast('Hairy Potter') Movie Hairy Potter is not found
Your function should take advantage of the power of the dictionary structure and the list structure. You do not need to include more than one for loop in the body of this function.
You function should call findActor
.
Test your procedure and fix it if it doesn't work.
Submit your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit one Python file called
myIMDb
.
Be sure that your module can run on the computer lab machines.
If you submit your project early, you can get an early feedback on the accuracy of your work.
The grader will test your program against various input values. Check your work against the grading rubric project5gradesheet.pdf which the grader will use when grading your lab.