Untitled
unknown
python
a year ago
1.5 kB
8
Indexable
Never
class Shop(object): # discount amount # it will be subtract from the MRP of the product DISCOUNT_AMOUNT = 2500 def __init__(self): # products # name of the product and the price of the product self.products = { "hp": 49999, "lenovo": 56999, "acer": 44999 } self.TOTAL_PRODUCTS = len(self.products.keys()) def get_product_by_index(self, index: int): """ Returns product name and price in tuple by index entered by user """ index = index-1 PRODUCT_NAME = list(self.products.keys())[index] PRODUCT_PRICE = self.products[list(self.products.keys())[index]] return (PRODUCT_NAME, PRODUCT_PRICE) def start(self): """ start the shop """ i = 1 for k,v in self.products.items(): print("Product index:",i) print("Product name:",k) print("Product price:",v) print("-"*25) i += 1 index = None while not index or not index.isnumeric() or int(index) < 1 or int(index) > self.TOTAL_PRODUCTS: index = input("Enter product index:") PRODUCT_NAME, PRODUCT_PRICE = self.get_product_by_index(int(index)) print("You've purchased %s, for %.2f" %(PRODUCT_NAME, PRODUCT_PRICE-self.DISCOUNT_AMOUNT)) if __name__ == "__main__": shop = Shop() shop.start()