Python Forum
class - calculate total price of copies
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class - calculate total price of copies
#1
Hi! I can the below code, I want to calculate de total price of copies for a book

class Library():
    def __init__(self, name) -> None:
        self.book = {}
        self.name = name
        self.stock = ' '

    def lista_of_books(self):
        return self.book

    def append_book(self, title, author, copies, price):
         self.book.update(titlu=title, autor=author, exemplare=copies, pret1=price)

    def total_price_of_copies(self, title):
        for key,value in self.book.items():
            if self.book[key] == title:
                print('Pretul pentru toate exemplarele din', title, self.book['exemplare1'] * self.book['pret1'])
                break

class Book():
    def __init__(self, title, author, copies, price):
        self.titlu = title
        self.autor = author
        self.exemplare = copies
        self.pret = price

a = Library('Alexandria')
a.append_book('Howls moving castle', 'Diana Wynne Jones', 8, 33)
a.append_book('Graffiti Moon', 'Cath Crowley', 5, 28)
a.append_book('All the Bright Places', 'Jennifer Niven', 7, 15)

a.total_price_of_copies('Howls moving castle')
Reply
#2
There are a couple of places where your code is not doing what you think it's doing. I recommend inserting print methods in your code to see for example what the dictionarybookactually contains when you think it has all three books. Anyway... try this and see if it is what you were looking for.
class Library():
	def __init__(self, name) -> None:
		self.books = {}

	def lista_of_books (self) :
		print ()
		for title, information in self.books.items () :
			print ('Titlu:', title)
			for label, info in information.items () :
				print (f'\t{label}: {info}')

	def append_book (self, title, author, copies, price) :
		self.books [title] = {'autor': author, 'exemplare': copies, 'pret1': price}

	def total_price_of_copies (self, title) -> int :
		number_of_copies = self.books [title]['exemplare']
		price_per_copy = self.books [title]['pret1']
		return price_per_copy * number_of_copies

a = Library('Alexandria')
a.append_book('Howls moving castle', 'Diana Wynne Jones', 8, 33)
a.append_book('Graffiti Moon', 'Cath Crowley', 5, 28)
a.append_book('All the Bright Places', 'Jennifer Niven', 7, 15)

a.lista_of_books ()
print (f'\nThe price for all copies of "Howls moving castle"', end = '')
print (f' is ${a.total_price_of_copies ("Howls moving castle")}')
3lnyn0 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding the price based on industry and number of transactions chandramouliarun 0 914 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,717 Oct-30-2021, 11:20 PM
Last Post: Yoriz
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,183 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Calculate and print the total cost isha_dx 7 11,646 Feb-10-2021, 04:56 PM
Last Post: nilamo
  An important question is how to create a zigzag in price data? epsilon 0 1,312 Nov-18-2020, 08:06 PM
Last Post: epsilon
  How to create local copies of Python packages so they do not have to be downloaded okhajut 3 2,038 Sep-29-2020, 02:22 PM
Last Post: buran
  Property price calculation oli_action 4 3,178 Jul-15-2020, 04:27 PM
Last Post: sridhar
  price + tax rounding mlieqo 11 6,472 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  Stock Rate of Change (based on open and current price) Not Working firebird 1 2,379 Jul-29-2019, 07:10 AM
Last Post: perfringo
  Date for Parking price PyQt5 vbv 0 2,996 Apr-19-2017, 08:53 PM
Last Post: vbv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020