← Back to challenges

Book Shelf

PythonHardobjectslanguage_fundamentalsclasses

Instructions

Create a Book class that has two attributes:

  1. .title
  2. .author

and two methods:

  1. A method named .get_title() that returns: "Title: " + the instance title.
  2. A method named .get_author() that returns: "Author: " + the instance author.

and instantiate this class by creating 3 new books:

  1. Pride and Prejudice - Jane Austen (PP)
  2. Hamlet - William Shakespeare (H)
  3. War and Peace - Leo Tolstoy (WP)

The name of the new instances should be PP, H, and WP, respectively.

For instance, if I instantiated the following book using this Book class:

  • Harry Potter - J.K. Rowling (HP)

I would get the following attributes and methods:

Examples

HP.title ➞ "Harry Potter"
HP.author ➞ "J.K. Rowling"
HP.get_title() ➞ "Title: Harry Potter"
HP.get_author() ➞ "Author: J.K. Rowling"

Notes

  • Remember, after you've finished writing the class and its methods, you must instantiate it through the creation of new objects.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: The Fifth Argument