Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Edward Randall
Edward Randall
1,834 Points

What is wrong with my code?

class Book: def init(self, name, date, author): self.name = name self.date = date self.author = author

def str(self): return f"{self.name} {self.date} {self.author}"

class shelf: def init(self): self.place = []

def iter(self): yield from self.place

def add_book(self, place): self.place.append(place)

some = Book("poop", "boob", "fine")

shelf.add_book("ebby", "Jan 27th")

I am trying to have the two classes interact with each other using dunder methods. I want to be able to add a "book" to the book shelf

Steven Parker
Steven Parker
217,443 Points

You might want to take a look at these videos about Posting a Question, using Markdown formatting to preserve the code's appearance, and this one about sharing a snapshot of your workspace.

1 Answer

Steven Parker
Steven Parker
217,443 Points

Without seeing the code properly formatted, there could be other issues; but one standout is that a class should not refer to itself by name. So instead of "shelf.add_book", you probably want "self.add_book".

Also, why would the code create a book named "some", but then not use it?

And it looks like "add_book" is being called with 2 arguments, but it's only defined to take one.