Build a Media Catalogue - Step 41

Tell us what’s happening:

I have tried numerous different way’s and keep getting same error I am now completely stuck, could some one please point me in right direction

Your code so far

class Movie:
    """Parent class representing a movie."""
    def __init__(self, title, year, director, duration):
        if not title.strip():
            raise ValueError("Title cannot be empty")
        if year < 1895:
            raise ValueError("Year must be 1895 or later")
        if not director.strip():
            raise ValueError("Director cannot be empty")
        if duration <= 0:
            raise ValueError("Duration must be positive")
        
        self.title = title
        self.year = year
        self.director = director
        self.duration = duration
    
    def __str__(self):
        return f"{self.title} ({self.year}) - {self.duration} min, {self.director}"

class TVSeries(Movie):
    """Child class representing an entire TV series."""
    def __init__(self, title, year, director, duration, seasons, total_episodes):
        super().__init__(title, year, director, duration)
        if seasons < 1:
            raise ValueError("Seasons must be 1 or greater")
        if total_episodes < 1:
            raise ValueError("Total episodes must be 1 or greater")
        
        self.seasons = seasons
        self.total_episodes = total_episodes
    
    def __str__(self):
        return f"{self.title} ({self.year}) - {self.seasons} seasons, {self.total_episodes} episodes, {self.duration} min avg, {self.director}"

class MediaError(Exception):
    """Custom exception for media-related errors."""
    def __init__(self, message, obj):
        super().__init__(message)
        self.obj = obj

class MediaCatalogue:
    """A catalogue that can store different types of media items."""
    def __init__(self):
        self.items = []
    
    def add(self, media_item):
        if not isinstance(media_item, Movie):
            raise MediaError("Only Movie or TVSeries instances can be added", media_item)
        self.items.append(media_item)
    
    def get_movies(self):
        return [item for item in self.items if type(item) == Movie]
    
    def get_tv_series(self):
        return [item for item in self.items if type(item) == TVSeries]
    
    def __str__(self):
        if not self.items:
            return "Media Catalogue (empty)"
        movies = self.get_movies()
        series = self.get_tv_series()

# User Editable Region

        result = f"Media Catalogue ({len(self.items)} items):\n\n"
        
        # Display movies section
        if movies:
            result += "=== MOVIES ===\n"
            for i, item in enumerate(movies, 1):
                result += f"{i}. {item}\n"
        
        # Also display TV series (maybe this is what's missing)
        if series:
            for i, item in enumerate(series, len(movies) + 1):
                result += f"{i}. {item}\n"
        
        return result

catalogue = MediaCatalogue()

try:
    movie1 = Movie('The Matrix', 1999, 'The Wachowskis', 136)
    catalogue.add(movie1)
    movie2 = Movie('Inception', 2010, 'Christopher Nolan', 148)
    catalogue.add(movie2)
    series1 = TVSeries('Breaking Bad', 2008, 'Vince Gilligan', 3024, 5, 62)

# User Editable Region

    catalogue.add(series1)
    series2 = TVSeries('Game of Thrones', 2011, 'David Benioff', 4224, 8, 73)
    catalogue.add(series2)
except ValueError as e:
    print(f"Validation Error: {e}")
except MediaError as e:
    print(f"Media Error: {e}")
    print(f'Unable to add {e.obj}: {type(e.obj)}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Media Catalogue - Step 41

I think you’ve modified the existing for loop that was asked to indent .

You had :

        if movies:
            result += '=== MOVIES ===\n'
        for i, movie in enumerate(self.items, 1):
            result += f'{i}. {movie}\n'

and now you have :

        if movies:
            result += "=== MOVIES ===\n"
            for i, item in enumerate(movies, 1):
                result += f"{i}. {item}\n"

Do you see any difference ?

yeah i see the difference but the challenge explicitly says to indent the for statement and change self.items to movies that is what is totally confusing me

Were you asked to write this code? Are you using AI to write your code?

I ran this through AI as i’ve spent around 5 hours trying to figure it out. I use all the tools available to write my code books, the internet and AI is a last resort as I don’t really like using it i prefer to learn from books and the internet

have you tried not changing the loop variable? doing things you are not asked to do can cause test failure

Just be aware that this is one of the forum rules:

Please do not share AI-generated content within this community.

Don’t use LLM to write code for you. You’ll never learn that way.

Reset the step to get the original code back and remove all the extra things you added.

Indent your existing for loop by one level so that it runs when the catalogue has movies in it. Then, replace self.items with movies to iterate over movies only.

Just do these two things, nothing else.

  1. Indent your existing for loop by one level
  2. replace self.items with movies

That’s all no other changes.

Ask here if you have any questions about the instructions or code.