/
stendal
/
Files
Обзор
Документация
Войти
/
stendal
/
Files
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
patch
cook_book.py
53 строки
2 KB
stendal
upload files
04 июл 2025, 23:39
04 июл 2025, 23:39
aad57bd
Код
Авторство
О чём код?
from pprint import pprint cook_book = { 'Омлет': [ {'ingredient_name': 'Яйцо', 'quantity': 2, 'measure': 'шт.'}, {'ingredient_name': 'Молоко', 'quantity': 100, 'measure': 'мл'}, {'ingredient_name': 'Помидор', 'quantity': 2, 'measure': 'шт'} ], 'Утка по-пекински': [ {'ingredient_name': 'Утка', 'quantity': 1, 'measure': 'шт'}, {'ingredient_name': 'Вода', 'quantity': 2, 'measure': 'л'}, {'ingredient_name': 'Мед', 'quantity': 3, 'measure': 'ст.л'}, {'ingredient_name': 'Соевый соус', 'quantity': 60, 'measure': 'мл'} ], 'Запеченный картофель': [ {'ingredient_name': 'Картофель', 'quantity': 1, 'measure': 'кг'}, {'ingredient_name': 'Чеснок', 'quantity': 3, 'measure': 'зубч'}, {'ingredient_name': 'Сыр гауда', 'quantity': 100, 'measure': 'г'}, ] } def get_shop_list_by_dishes(dishes, person_count): """ Calculate the number of ingredients for the number of people. Param: dishes (list): dishes in the cookbook person_count (int): number of persons Return (dict): the number of ingredients depends on the number of people or a message about a dish not being in the cookbook """ shop_list = {} for dish in dishes: if dish in cook_book: for ingredient in cook_book[dish]: name = ingredient['ingredient_name'] measure = ingredient['measure'].rstrip('.') quantity = ingredient['quantity'] * person_count if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} else: print(f'Блюда "{dish}" нет в кулинарной книге') return shop_list # Usage example result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2) pprint(result)