The value of the inventory of a particular book is the price of that book times the number of copies we have in stock. Write a function called inventory_value that takes a Book as its argument and returns the value of our inventory of that book. Then write a function called top_value that takes a list of Books as its argument and returns the Book object (from the list) that has the highest-vaue inventory. Finally, write a sequence of statements that prints a line in this form: The highest-value book is War and Peace by Tolstoy, Leo at a value of $ 595.00.
So far, I've written this:
from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
# Book attributes: author, title of book, category of book, year of publication, and number of copies available
BSI = [
Book('George Orwell', 'Animal Farm', 'Fiction', 1945, 9.99, 21),
Book('J.K. Rowling', 'Harry Potter and the Deathly Hallows', 'Fantasy', 2007, 24.26, 32),
Book('J.R.R. Tolkien', 'The Fellowship of the Ring', 'Fantasy', 1954, 10.87, 26),
Book('Toni Morrison', 'The Bluest Eye', 'Fiction', 1970, 11.02, 13),
Book('Ernesto Galarza', 'Barrio Boy', 'Autobiography', 1971, 13.50, 23),
Book('Stephen Chbosky', 'The Perks of Being a Wallflower', 'Fiction', 1999, 8.01, 25)]
def inventory_value(b: Book)->str:
return (b.price * b.instock)
assert(inventory_value(BSI[1])) == 776.32
assert(inventory_value(BSI[2])) == 282.62
def top_value(b: Book)->str:
for top_value in BSI:
if (b.price * b.instock) == max(inventory_value):
return(top_value.title)
print(top_value(BSI))
It's my function of top_value that isn't working out for me, but I can't tell what I'm doing wrong. I'm new to this and I'd like for someone to explain how I can go about doing this part of the problem. Thanks!