/
Patient0
/
Home_Work
Обзор
Документация
Войти
/
Patient0
/
Home_Work
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task_1_cookbook.py
45 строк
1 KB
Patient0
upload files
12 дек 2025, 16:55
12 дек 2025, 16:55
f1f3357
Код
Авторство
О чём код?
def read_cook_book(file_path): """ Считывает рецепты из файла и возвращает словарь cook_book в формате: { 'Название блюда': [ {'ingredient_name': ..., 'quantity': ..., 'measure': ...}, ... ], ... } """ cook_book = {} with open(file_path, encoding='utf-8') as file: while True: dish_name = file.readline().strip() if not dish_name: break ingredient_count = int(file.readline().strip()) ingredients = [] for _ in range(ingredient_count): line = file.readline().strip() if not line: break ingredient_name, quantity_str, measure = line.split(' | ') ingredients.append({ 'ingredient_name': ingredient_name, 'quantity': int(quantity_str), 'measure': measure }) cook_book[dish_name] = ingredients # Пропускаем пустую строку между рецептами (если есть) file.readline() return cook_book if __name__ == '__main__': cook_book = read_cook_book('recipes.txt') from pprint import pprint pprint(cook_book, sort_dicts=False)