/
Efsilo
/
HomeWork
Обзор
Документация
Войти
/
Efsilo
/
HomeWork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Mission2.py
51 строка
2 KB
Efsilo
M2
16 авг 2025, 10:11
16 авг 2025, 10:11
98c09fd
Код
Авторство
О чём код?
def read_cookbook(file_path): cook_book = {} with open(file_path, encoding='utf-8') as file: while True: dish_name = file.readline().strip() if not dish_name: break ingridient_count = int(file.readline().strip()) ingridients = [] for _ in range(ingridient_count): ingridient_line = file.readline().strip() ingridient_data = ingridient_line.split(' | ') ingridient = { 'ingridient_name' : ingridient_data[0], 'quantity' : int(ingridient_data[1]), 'measure' : ingridient_data[2] } ingridients.append(ingridient) cook_book[dish_name] = ingridients file.readline() return cook_book def get_shop_list_by_dishes(dishes, person_count, cook_book): shop_list = {} for dish in dishes: if dish in cook_book: for ingridient in cook_book[dish]: name = ingridient['ingridient_name'] measure = ingridient['measure'] quantity = ingridient['quantity'] * person_count if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = {'quantity': quantity, 'measure': measure} else: print(f'Блюдо {dish} не найдено.') return shop_list cook_book = read_cookbook('recipes.txt') print(cook_book) shop_list = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2, cook_book) print(shop_list)