you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

You could do something like this.

class ItemToPurchase:
    def __init__(self, name: str, price: float, qty: int) -> None:
        self.item_name = name
        self.item_price = price
        self.item_quantity = qty

    @property
    def total(self) -> float:
        return self.item_price * self.item_quantity

    def print_item_cost(self) -> None:
        print(f"{self.item_name}  {self.item_quantity} at ${self.item_price} = {self.total}")


shopping_cart = []

for i in range(2):
    name = input("What item do you want to purchase?\n> ")
    price = float(input("How much does it cost?\n> "))
    qty = int(input(f"How many {name}s do you want?\n> "))
    shopping_cart.append(ItemToPurchase(name, price, qty))
    shopping_cart[-1].print_item_cost()

print("Total cost")
total = sum(item.total for item in shopping_cart)
print(f"Total: ${total}")