Untitled
user_3592770
plain_text
2 years ago
786 B
13
Indexable
class Warehouse: def __init__(self): self.products = {} def add_product(self, name, price, quantity): if name in self.products: self.products[name]["quantity"] += quantity else: self.products[name] = {"price": price, "quantity": quantity} def __repr__(self): product_list = [] for name, product in self.products.items(): product_list.append(f"{name}: €{product['price']}, {product['quantity']} available") return "\n".join(product_list) # create a new Warehouse instance w = Warehouse() # add some products w.add_product("laptop-lenovo",350 , 100) w.add_product("laptop-apple", 900, 250) w.add_product("laptop-dell", 500, 50) # print the warehouse print(w)
Editor is loading...