In: Computer Science
CarrieMovies = {"Titanic":3.5, "Mean Girls":4.9, "Finding Nemo":4.7, "Wonder Woman": 4.3}
LeslieMovies = {"Clueless": 3.2, "Spy Kids":4.1, "Black Panther":5.0}
def topMovie():
return
CarriesFav = topMovie(CarrieMovies)
LesliesFav = topMovie(LeslieMovies)
print("Carrie's Fav Movie: ",CarriesFav)
print("Leslie's Fav Movie: ",LesliesFav)
_Complete the function topMovie(). This program is not complete.
The purpose of the function is to determine the title of the highest rated movie in a dictionary. The starter file defines two dictionaries of favorite films and their associated ratings. The function should be dynamic and able to return the title of the highest rated movie for any dictionary passed to it!
output is:
Carrie's Fav Movie: Mean Girls
Leslie's Fav Movie: Black Panther
Here is the complete program for the given question.
Sample output is added at the end.
Code:
CarrieMovies = {"Titanic":3.5, "Mean Girls":4.9, "Finding Nemo":4.7, "Wonder Woman": 4.3}
LeslieMovies = {"Clueless": 3.2, "Spy Kids":4.1, "Black Panther":5.0}
#function that returns the title of highest rated movie
def topMovie(movies):
movie_ratings=list(movies.values()) #list of all ratings in the dictionary
max_rating=max(movie_ratings) #computes the highest rating
top_movies=[] #list of highest rated movies
for movie in movies.keys(): #for each movie in dictionary
if movies[movie]==max_rating: #if rating of the movie equals highest rating
top_movies.append(movie) #add the movie to top_movies list
if len(top_movies)==1: #if there is only one highest rated movie
return top_movies[0] #return the movie name
else:
return top_movies #return the list of movie names
CarriesFav = topMovie(CarrieMovies)
LesliesFav = topMovie(LeslieMovies)
print("Carrie's Fav Movie: ",CarriesFav)
print("Leslie's Fav Movie: ",LesliesFav)
Output: