MCS-177 Project 5: Your IMDb (Spring 2016)

Start: Thursday 3/17; Due: Monday 4/4, by the beginning of class


Overview

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.

Specific tasks

  1. 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.

  2. Global variables are variables you can refer to anywhere in Python. For example, if you defined 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:

    {"Harry Potter and the Sorcerer's Stone":{'Harry Potter':'Daniel Radcliffe', 'Ron Weasley':'Rupert Grint', 'Albus Dumbledore':'Richard Harris', 'Hermione Granger':'Emma Watson'}}

    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.

  3. Write the contract, docstring and implementation for a function 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.

  4. Use the function 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.

  5. Write the contract, docstring and implementation for a function 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.
  6. 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
    
        ''
    
    
  7. 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 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
    
    

Submitting your work

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:

Grading

You will earn one point for each of the following accomplishments:

  1. You wrote a correct contract and docstring for the function makeDictionary.

  2. Your makeDictionary meets the stated expectations.

  3. You defined the variable myIMDb and initialized it to an empty dictionary.

  4. You wrote a correct contract and docstring for the function addMovie.

  5. The function addMovie correctly adds a new movie entry to the global variable myIMDb.

  6. The function addMovie makes use of the function makeDictionary.

  7. You use the function addMovie correctly at least 3 times.

  8. You wrote a correct contract and docstring for the function listMovies.

  9. The function listMovies returns a list of titles of all the movies in the global variable myIMDb.

  10. You wrote a correct contract and docstring for the function findActor.

  11. The function findActor looks up the correct actor/actress for the given role in the given movie.

  12. The function findActor prints out the error messages and returns an empty string correctly.

  13. You wrote a correct contract and docstring for the function showCast.

  14. The function showCast prints the header of the table.

  15. The function showCast lists all the characters and actors/actresses from the right movie.

  16. The function showCast lists all the characters and actors/actresses in the alphabetical order.

  17. The function showCast formats the table correctly.

  18. The function showCast prints out the error message when the movie is not found.

  19. You use descriptive names for parameters and variables used in your functions.

  20. Your functions are not unnecessarily complex.