product
unknown
python
2 years ago
1.4 kB
6
Indexable
import json class Product: def __init__(self): self.data = [] while True: self.name = input("Product: ") self.price = float(input("Price: ")) end = input("Continue Y/N: ") if self.price < 0: print("Invalid price") self.price = 0 self.data.append([self.name, self.price]) if end in ['Y', 'y']: continue else: break def print_info(self): for d in self.data: print(d) class ProductList: def __init__(self, product): self.__filename = 'data.json' data = [] for p in product: data.append( { "name":p[0], "price":p[1] } ) with open(self.__filename, 'w') as file: json.dump(data, file, indent=4) def readFromFile(self): with open(self.__filename, 'r') as file: data = json.load(file) for values in data: print(f"Product: {values['name']}, Price: {values['price']}") if __name__ == '__main__': p1 = Product() ProductList(p1.data).readFromFile()
Editor is loading...