Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Question 1: Begin with the class below and do the following: a) Modify the whattowatch…

  1. User

    Question 1: Begin with the class below and do the following:

    a) Modify the what_to_watch method so that it takes an optional keyword

    argument that allows the user to narrow down the random selection by

    category (e.g. select only from movies with category 'action'), but

    defaults to the entire list of titles as it does now.

    b) The what_to_watch method currently raises a ValueError if you use it

    before entering any movies. Modify it using try/except so that it tells

    the user what they did wrong instead of raising an error.

    c) Create a new class called InteractiveMovieDataBase that inherits MovieDataBase.

    d) Override the add_movie method in your new class so that if it is called

    without arguments, it instead asks for user input to add a title/year/

    category/rating/stars, but if it is called with arguments it behaves as before

    e) Add some appropriate error checking to InteractiveMovieDatabase on the user

    input, so that they can't enter something that makes no sense (e.g. title=None

    or year='dog')

    f) Add a new method to InteractiveMovieDataBase named movie_rankings, which

    returns a list of all the titles in the database currently, ordered

    highest ranking (by stars) to lowest

    NOTE: Your final submission should have only TWO classes: one (modified)

    MovieDataBase, and the new InteractiveMovieDataBase

    from numpy import random

    class MovieDataBase(): def init(self): self.titles = [] self.movies = {}

    def add_movie(self, title, year, category, rating, num_stars):
        self.titles.append(title)
        self.movies[title] = {'year':year, 'category':category, 'rating':rating, 'stars':num_stars}
        print(f'{title} ({year}) added to the database.')
    
    def what_to_watch(self):
        choice = random.choice(self.titles)
        movie = self.movies[choice]
        print(f"Your movie today is {choice} ({movie['year']}), which is a {movie['rating']}-rated {movie['category']}, and was given {movie['stars']} stars.")
    
  2. Assistant

    from numpy import random

    class MovieDataBase(): def init(self): self.titles = [] self.movies = {}

    def add_movie(self, title=None, year=None, category=None, rating=None, num_stars=None):
        if title is None:
            title = input("Enter movie title: ")
        if year is None:
            year = input("Enter movie year: ")
        if category is None:
            category = input("Enter movie category: ")
        if rating is None:
            rating = input("Enter movie rating: ")
        if num_stars is None:
            num_stars = input("Enter movie stars: ")
    
        if title and year and category and rating and num_stars:
            self.titles.append(title)
            self.movies[title] = {'year':year, 'category':category, 'rating':rating, 'stars':num_stars}
            print(f'{title} ({year}) added to the database.')
        else:
            print("Invalid movie information entered.")
    
    def what_to_watch(self, category=None):
        try:
            if not self.titles:
                raise ValueError("No movies in the database.")
            
            if category is not None:
                category_movies = [title for title in self.titles if self.movies[title]['category'] == category]
                
                if not category_movies:
                    raise ValueError(f"No movies in the '{category}' category.")
                
                choice = random.choice(category_movies)
            else:
                choice = random.choice(self.titles)
            
            movie = self.movies[choice]
            print(f"Your movie today is {choice} ({movie['year']}), which is a {movie['rating']}-rated {movie['category']}, and was given {movie['stars']} stars.")
        
        except ValueError as err:
            print(err)
    

    class InteractiveMovieDataBase(MovieDataBase): def add_movie(self, title=None, year=None, category=None, rating=None, num_stars=None): if title is None: title = input("Enter movie title: ") if year is None: year = input("Enter movie year: ") if category is None: category = input("Enter movie category: ") if rating is None: rating = input("Enter movie rating: ") if num_stars is None: num_stars = input("Enter movie stars: ")

        if title and year and category and rating and num_stars:
            self.titles.append(title)
            self.movies[title] = {'year':year, 'category':category, 'rating':rating, 'stars':num_stars}
            print(f'{title} ({year}) added to the database.')
        else:
            print("Invalid movie information entered.")
    
    def movie_rankings(self):
        sorted_titles = sorted(self.titles, key=lambda title: self.movies[title]['stars'], reverse=True)
        return sorted_titles
    
        
    

    Example usage:

    database = InteractiveMovieDataBase()

    database.add_movie(title="The Shawshank Redemption", year="1994", category="drama", rating="R", num_stars="9.3") database.add_movie(title="Pulp Fiction", year="1994", category="crime", rating="R", num_stars="8.9") database.add_movie(title="Titanic", year="1997", category="romance", rating="PG-13", num_stars="7.8") database.add_movie(title="The Dark Knight", year="2008", category="action", rating="PG-13", num_stars="9.0")

    database.what_to_watch(category="action") database.what_to_watch()

    print(database.movie_rankings())