/
STRATOCASTER
/
Homework-API
Обзор
Документация
Войти
/
STRATOCASTER
/
Homework-API
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task_2.py
44 строки
1 KB
STRATOCASTER
Загружен материал
22 мар 2026, 23:05
Верифицирован
22 мар 2026, 23:05
332f6d9
Код
Авторство
О чём код?
import pprint cook_book = {} with open('recipes.txt', encoding='utf-8') as f: for line in f: dish_name = line.strip() if not dish_name: continue ingredient_count = int(f.readline().strip()) ingredients = [] for i in range(ingredient_count): name, quantity, measure = f.readline().strip().split(' | ') ingredients.append({ "ingredient_name": name, "quantity": int(quantity), "measure": measure }) cook_book[dish_name] = ingredients f.readline() def get_shop_list_by_dishes(dishes, person_count): shop_list = {} for dish in dishes: if not dish in cook_book: print(f'Блюда {dish} нет в книге.') else: for ingredient_dict in cook_book[dish]: name = ingredient_dict["ingredient_name"] if not name in shop_list: shop_list[name] = { "quantity": ingredient_dict["quantity"] * person_count, "measure": ingredient_dict["measure"] } else: shop_list[name]["quantity"] += ingredient_dict["quantity"] * person_count return shop_list result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2) pprint.pprint(result)