The purpose of this program is to recommend a movie to the user based on the highest average ratings in a genre. The program prompts the user to provide ratings (on a scale of 1-5) for a predefined list of movies. It calculates the average rating for each genre based on user inputs and recommends movies from the genre with the highest average rating.
The program uses a list of dictionaries (movies
) to store data about movies, including their titles, genres, and user-provided ratings. Each dictionary represents a movie and includes keys like title
, genre
, and rating
. The rating
key holds a list of user ratings, which allows multiple ratings to be stored and averaged. This abstraction simplifies the process of managing movies and their associated data, as all relevant information for each movie is encapsulated in a single dictionary within the list.
The use of the movies
list and its dictionary elements simplifies managing movie-related data by grouping the title, genre, and ratings together for each movie. Instead of handling separate variables for each piece of data, the program accesses and updates all related data through the dictionary keys. This approach reduces complexity by organizing related data in a single structure and enabling easy iteration through the list when collecting ratings or calculating averages.
The calculate_genre_averages
function is an example of procedural abstraction. It takes no parameters and calculates the average ratings for each genre based on the user-provided ratings. This function separates the logic for calculating averages from the rest of the program, making the code modular and reusable. By isolating this functionality, it ensures that the genre averages can be calculated consistently and reused elsewhere in the program (e.g., in recommend_movie
).
The main algorithm works as follows:
get_user_ratings
function iterates through the movies
list and prompts the user to input a rating (1-5) for each movie. Validated ratings are appended to the movie's rating
list.calculate_genre_averages
function calculates the average rating for each genre. It iterates through the movies
list, sums the ratings for each genre, and counts the number of ratings. It then computes the average by dividing the total rating by the count for each genre.recommend_movie
function identifies the genre with the highest average rating and lists all movies from that genre as recommendations.Test 1: User rates movies equally
Input:
Inception: 5 The Godfather: 5 The Dark Knight: 5 Interstellar: 5 Pulp Fiction: 5
Output:
Recommended Genre: Sci-Fi Movies you might enjoy: - Inception - Interstellar
Test 2: User rates one genre higher
Input:
Inception: 5 The Godfather: 2 The Dark Knight: 4 Interstellar: 5 Pulp Fiction: 2
Output:
Recommended Genre: Sci-Fi Movies you might enjoy: - Inception - Interstellar