Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to make a method in a class in Python that will take values from a a dictionary and return the original price's, the total amount of tax and the total of all items plus tax.
like this,
iPad.....$799.99
Surface..$999.99
Tax......$108.00
Total....$1907.98

my code is below and my methods for tax are def sales_tax and def calculated_tax.

What I have tried:

Python
class Invoice:
    def __init__(self, invoice_id, customer_id, last_name, first_name, phone_number, address):
        self.invoice_id = invoice_id
        self.customer_id = customer_id
        self.last_name = last_name
        self.first_name = first_name
        self.phone_number = phone_number
        self.address = address
        self.items_with_price = {}

    def add_item(self, product, price):
        if product not in self.items_with_price:
            self.items_with_price["iPad"] = 799.99
            self.items_with_price["Surface"] = 999.99

    def sales_tax(self, price):
        sales_tax = .06
        return items_with_price * sales_tax

    def calculated_tax(self, price):
        self.calculated_tax = sales_tax * items_with_price

    def display_totals(self, price):
        print('iPad', price)
        print('Surface', price)
        sales_tax = calculated_tax(price)
        print('Tax', calculated_tax)
        print('Total', price + calculated_tax)

    def __str__(self):
        return(self.items_with_price)

# Driver code
invoice  = Invoice(1, 123, '1313 Disneyland Dr, Anaheim, CA 92802', 'Mouse', 'Minnie', '555-867-5309')
invoice.add_item({'iPad': 799.99})
invoice.add_item({'Surface': 999.99})
invoice.create_invoice()
Posted
Updated 2-Nov-22 9:46am
v3
Comments
OriginalGriff 2-Nov-22 15:47pm    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 2-Nov-22 16:25pm    
You could be keeping and running total and tax amount - when adding a new item (except your "add_item" doesn't look quite right).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900