Here\'s the question that I\'m supposed to code for:
Write the contract, docstring and implementation for a function showCast that takes a movie title and
First, i don't think you should return anything in the addMovie
function. Just simply add it to the global variable:
myIMDb = {}
def addMovie (title, charList, actList):
global 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"""
dict2 = {}
for i in range (0, len(charList)):
dict2 [charList[i]] = actList[i]
myIMDb[title] = dict2
Although i don't recommend to use global variables often, i think it's forgivable in this case :D
After that, in your showCast
function, i'd use this:
def showCast(title):
if title in myIMDb:
actList=[]
chList=[]
movie = myIMDb[title]
for character, cast in movie.keys(), movie.values(): #grab the character from
#the keys, and cast from the values.
chList.append(character)
actList.append(cast)
print (chList, actList)
else:
return "Movie not Found"
Here's my output:
['Columbus', 'Jesse Eisenberg, '] ['Wichita', 'Emma Stone']
It's working as expected, hope this helps!
Maybe these snippets help you:
Printing a table row
def printRow(character, actor):
print(character + (20 - len(character)) * ' ' + actor))
Sorting the characters
def getSortedTitleList(titleList):
sortedList = []
characters = sorted(titleList)
for character in characters:
sortedList.append((character, titleList[character]))
return sortedList
Note: I changed dict.keys().sort()
to sorted(dict)
to be compatible with Python 3.