/
yagario
/
Task2
Обзор
Документация
Войти
/
yagario
/
Task2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task2.py
44 строки
2 KB
yagario
За
04 июл 2025, 12:51
04 июл 2025, 12:51
383c386
Код
Авторство
О чём код?
def read_cook_book(filename): cook_book = {} with open(filename, encoding='utf-8') as file: while True: dish_name = file.readline().strip() if not dish_name: break ingredients_count = int(file.readline()) ingredients = [] for _ in range(ingredients_count): ingredient_line = file.readline().strip() name, quantity, measure = [x.strip() for x in ingredient_line.split('|')] ingredients.append({ 'ingredient_name': name, 'quantity': int(quantity), 'measure': measure }) cook_book[dish_name] = ingredients file.readline() # пустая строка-разделитель return cook_book def get_shop_list_by_dishes(dishes, person_count, cook_book): shop_list = {} for dish in dishes: if dish not in cook_book: print(f'Блюдо "{dish}" не найдено в книге рецептов.') continue for ingredient in cook_book[dish]: name = ingredient['ingredient_name'] measure = ingredient['measure'] quantity = ingredient['quantity'] * person_count if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} return shop_list cook_book = read_cook_book('recipes.txt') from pprint import pprint result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2, cook_book) pprint(result)