/
Sid90
/
PYAPI_Homework
Обзор
Документация
Войти
/
Sid90
/
PYAPI_Homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
62 строки
2 KB
Igor Sosin
Initial commit
25 авг 2025, 20:14
25 авг 2025, 20:14
2915d58
Код
Авторство
О чём код?
def parse_cookbook(filepath): cook_book = {} with open(filepath, encoding='utf-8') as file: lines = file.readlines() current_dish = None current_ing_quantity = 0 for line in lines: line = line.strip() if not line: continue if current_dish is None: current_dish = line elif current_ing_quantity == 0: current_ing_quantity = int(line) else: ingredient_name, quantity, measure = line.split(' | ') ingredient = { 'ingredient_name': ingredient_name, 'quantity': int(quantity), 'measure': measure } if current_dish not in cook_book: cook_book[current_dish] = [] cook_book[current_dish].append(ingredient) current_ing_quantity -= 1 if current_ing_quantity == 0: current_dish = None return cook_book cook_book = parse_cookbook('recipes.txt') def get_shop_list_by_dishes(dishes, person_count): shop_list = {} for dish in dishes: if dish in cook_book: for ingredient in cook_book[dish]: ingredient_name = ingredient['ingredient_name'] quantity = ingredient['quantity'] * person_count measure = ingredient['measure'] if ingredient_name not in shop_list: shop_list[ingredient_name] = { 'measure': measure, 'quantity': quantity } else: shop_list[ingredient_name]['quantity'] += quantity return shop_list dishes = ['Запеченный картофель', 'Омлет'] person_count = 2 result = get_shop_list_by_dishes(dishes, person_count)