Build a Media Catalogue - Step 13

Tell us what’s happening:

Build a Media Catalogue - Step 13
I get the following error text:

// running tests
4. When self.items contains Movie(‘Dances with Wolves’, 1990, ‘Kevin Costner’, 224) and Movie(‘Annie Hall’, 1977, ‘Woody Allen’, 93), your str method should return Media Catalogue (2 items):\n\n1. Dances with Wolves (1990) - 224 min, Kevin Costner\n2. Annie Hall (1977) - 93 min, Woody Allen\n.
5. When self.items contains Movie(‘Barry Lyndon’, 1975, ‘Stanley Kubrick’, 184) and Movie(‘Fahrenheit 451’, 1966, 'F

Your code so far

class 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 MediaCatalogue:
    def __init__(self):
        self.items = []

    def add(self, media_item):
        self.items.append(media_item)

# User Editable Region

    def __str__(self):
        if not self.items:
            return 'Media Catalogue (empty)'
        result = f'Media Catalogue ({len(self.items)} items):\n\n'
        for i, movie in enumerate(self.items , 1):
            result += f"{i}. {self.title} ({self.year} - {self.duration} min, {self.director}\n)"
        return result
        

# User Editable Region

try:
    movie1 = Movie('The Matrix', 1999, 'The Wachowskis', 136)
except ValueError as e:
    print(f'Validation Error: {e}')

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

Build a Media Catalogue - Step 13

Ok, and what debugging have yo tried?

Did you try calling your methods to see what happens?

That error text is copied from FreeCodeCamps’ terminal.

I run my code in the Spyder-Ide and the output was this;

runfile(‘/home/jajarvin/.config/spyder-py3/jj/Buil_a_Media_Catalog_jj.py’, wdir=‘/home/jajarvin/.config/spyder-py3/jj’)
Media Catalogue (empty)
Media Catalogue (1 items):

  1. Dances with Wolves (1990) - 224 min, Kevin Costner

Media Catalogue (2 items):

  1. Dances with Wolves (1990) - 224 min, Kevin Costner
  2. Annie Hall (1977) - 93 min, Woody Allen

Correction! This was Spyder’s output when there was two items in self.items :

runfile(‘/home/jajarvin/.config/spyder-py3/jj/Buil_a_Media_Catalog_jj.py’, wdir=‘/home/jajarvin/.config/spyder-py3/jj’)
Media Catalogue (2 items):

  1. Dances with Wolves (1990) - 224 min, Kevin Costner
  2. Annie Hall (1977) - 93 min, Woody Allen

Didn’t you already create a string representation of a movie object? Why not use that? Not great to repeat code like this.

That said are you sure the code here is exactly the same as your other editor?

Thank you for your tip.

I modified my code and now it gives the correct output

1 Like