Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,108 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
231,108 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.