/
Avinas71
/
M_7_1
Обзор
Документация
Войти
/
Avinas71
/
M_7_1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Module_7_1
31 строка
996 B
Avinas71
create Module_7_1
12 дек 2024, 13:23
12 дек 2024, 13:23
80996e4
Код
Авторство
О чём код?
from pprint import pprint class Product: def __init__(self, name, weight, category): self.name = name self.weight = float(weight) self.category = category def __str__(self): return f'{self.name}, {self.weight}, {self.category}' class Shop: __file_name = 'products.txt' def get_products(self): file = open(self.__file_name, 'r') products = file.read() file.close() return products def add(self, *products): current_products = self.get_products() file = open(self.__file_name, 'a') for product in products: if str(product) not in current_products: file.write(str(product) + '\n') current_products += str(product) + '\n' else: product_name = str(product).split(', ')[0] print(f'Продукт {product_name} уже есть в магазине') file.close()