/
Bacek
/
Homework_File
Обзор
Документация
Войти
/
Bacek
/
Homework_File
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
Homework_read_file.py
36 строк
1 KB
Vasiliy Dzina
Initial commit
08 май 2025, 15:54
08 май 2025, 15:54
692c628
Код
Авторство
О чём код?
with open('recipes.txt', encoding='UTF-8') as recipe: lines = (recipe.read().split('\n\n')) cook_book = {} for line in lines: dish, _, *ingredients = line.split('\n') ingredient_list = [] for ingr in ingredients: ingredient_name, quantity, measure = (ingr.split(' | ')) ingredient_list.append({'ingredient_name': ingredient_name, 'quantity': int(quantity), 'measure': measure}) cook_book[dish] = ingredient_list def shop_list_by_dishes(dishes, person_count): shop_list = {} for dish in dishes: if dish not in cook_book: print(f"Блюдо '{dish}' отсутствует в книге рецептов") continue for ingr in cook_book[dish]: name = ingr['ingredient_name'] quantity = ingr['quantity'] * person_count measure = ingr['measure'] if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} return shop_list import pprint pprint.pprint(shop_list_by_dishes(['Фахитос', 'Пельмени', 'Омлет'], 5))