Home > Net >  how do i print outputs like below for my class Book?
how do i print outputs like below for my class Book?

Time:01-16

Create a class called Book.

When called, description() should return the description.

Here is an example of how Book should work.

book_one = Book("1984", "George Orwell", 6.99)

print(book_one.description())

should print:

Title: 1984

Author: George Orwell

Price: £6.99

book_one = Book("1984", "George Orwell", 6.99, no_pages=328)

print(book_one.description())

should print:

Title: 1984

Author: George Orwell

Price: £6.99

No. of Pages: 328

book_one = Book("1984", "George Orwell", 6.99, year=1949)

print(book_one.description())

should print:

Title: 1984

Author: George Orwell

Price: £6.99

Year Published: 1949

book_one = Book("1984", "George Orwell", 6.99, no_pages=328, year=1949)

print(book_one.description()) should print:

Title: 1984

Author: George Orwell

Price: £6.99

Year Published: 1949

No. of Pages: 328

`class Book:

 def __init__(self, title, author, price,no_pages = None, year = None):

  self.title = title

  self.author = author

  self.price = price

  self.no_pages = no_pages

  self.year = year


 def description(self):

  print(f"Title: {self.title}")

  print(f"Author: {self.author}")

  print(f"Price: £{self.price}")

  print(f"No. of Pages: {self.no_pages}")

  print(f"Year: {self.year}")

  return

if __name__ == "__main__":

 book_one = Book("1984", "George Orwell", 6.99,328,year = 1949)
 book_one.description()`

CodePudding user response:

def description(self):
  return f"""Title: {self.title}

Author: {self.author}

Price: £{self.price}"""

CodePudding user response:

The more Pythonic approach to this is to override the __str__ dunder function. In that way you don't explicitly call a function to get the class contents in the format you need for presentation. The print() function will look for an implementation of __str__ in the object that it's processing. If that's absent then it will look for __repr__

Something like this:

class Book:
    def __init__(self, title, author, price, no_pages=None, year=None):
        self._title = title
        self._author = author
        self._price = float(price)
        self._no_pages = no_pages
        self._year = year
    def __str__(self):
        parts = [
            f'Title: {self._title}',
            f'Author: {self._author}',
            f'Price: £{self._price:.2f}'
        ]
        if self._year is not None:
            parts.append(f'Year Published: {self._year}')
        if self._no_pages is not None:
            parts.append(f'No. of Pages: {self._no_pages}')
        return '\n'.join(parts)


print(Book("1984", "George Orwell", 6.99), '\n')
print(Book("1984", "George Orwell", 6.99, no_pages=328), '\n')
print(Book("1984", "George Orwell", 6.99, year=1949), '\n')
print(Book("1984", "George Orwell", 6.99, no_pages=328, year=1949))

Output:

Title: 1984
Author: George Orwell
Price: £6.99 

Title: 1984
Author: George Orwell
Price: £6.99
No. of Pages: 328 

Title: 1984
Author: George Orwell
Price: £6.99
Year Published: 1949 

Title: 1984
Author: George Orwell
Price: £6.99
Year Published: 1949
No. of Pages: 328
  • Related