# movies.py # Demonstrates tuples through a movie score database movie_scores = [ ("Aquaman", 7.4), ("Mission: Impossible - Fallout", 7.9), ("Spider-Man: Into the Spider-Verse", 8.7), ("Mary Poppins Returns", 7.2), ("Avengers: Infinity War", 8.5), ("The Greatest Showman", 7.6), ("Black Panther", 7.4), ("Ready Player One", 7.5), ("Ralph Breaks the Internet", 7.3), ("Incredibles 2", 7.8)] def print_movies(movie_db): """ Given a movie database, it prints the movie and its corresponding score. :param movie_db: (list) a list of tuples that correspond to movies (str) and critic scores (float) :return: None """ for movie_pair in movie_db: (movie, score) = movie_pair print(movie + "\t" + str(score)) def print_movies2(movie_db): """ Given a movie database, it prints the movie and its corresponding score. Alternative implementation 2. :param movie_db: (list) a list of tuples that correspond to movies (str) and critic scores (float) :return: None """ for movie_pair in movie_db: print(movie_pair[0] + "\t" + str(movie_pair[1])) def print_movies3(movie_db): """ Given a movie database, it prints the movie and its corresponding score. Alternative implementation 3. :param movie_db: (list) a list of tuples that correspond to movies (str) and critic scores (float) :return: None """ for (movie, score) in movie_db: print(movie + "\t" + str(score)) def get_movie_score(movie_db, movie_title): """ Given a movie database and a movie title it returns its corresponding critic score, if it exists. :param movie_db: (list) a list of tuples that correspond to movies (str) and scores (float) :param movie_title: (str) the title of the movie to search for its critic score :return: (float) the critic score if found, or -1.0 if movie does not exist in database. """ for (movie, score) in movie_db: if movie == movie_title: return score return -1.0 def get_highest_rated_movie(movie_db): """ Returns the movie with the highest critic score in the provided database :param movie_db: (list) a list of tuples that correspond to movies (str) and scores (float) :return: (str) the movie with the highest critic score. """ max_score = -1 max_movie = "" for (movie, score) in movie_db: if score > max_score: max_score = score max_movie = movie return max_movie def get_movies_above_threshold(movie_db, threshold): """ Given a database and a threshold critic score, it returns a list of movies with scores above the threshold :param movie_db: (list) a list of tuples that correspond to movies (str) and scores (float) :param threshold: (num) the threshold critic score to filter movies by. :return: (list) a list of movie titles that have critic scores higher than the threshold """ movies_above = [] for (movie, score) in movie_db: if score >= threshold: movies_above.append(movie) return movies_above