/
SnakeMaster
/
Homework_R_W
Обзор
Документация
Войти
/
SnakeMaster
/
Homework_R_W
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
homework_read-write_2.py
40 строк
1 KB
Павел Боровиков
upload files
17 янв 2026, 13:52
Верифицирован
17 янв 2026, 13:52
d927597
Код
Авторство
О чём код?
def get_cook_book(): cook_book = {} with open('recipes.txt', 'r', encoding='UTF-8') as f: while True: name_of_dish = f.readline().strip() if not name_of_dish: break count = f.readline().strip() count = int(count) ingredients = [] for i in range(count): ingredient = f.readline().strip() separator_ = ingredient.split(' | ') dict = {'ingredient_name': separator_[0], 'quantity': int(separator_[1]), 'measure': separator_[2]} ingredients.append(dict) cook_book[name_of_dish] = ingredients f.readline() return cook_book def get_shop_list_by_dishes(dishes, person_count, cook_book): shop_list = {} for dish_name in dishes: ingredients = cook_book[dish_name] for ingredient in ingredients: name = ingredient['ingredient_name'] quantity = ingredient['quantity'] * person_count measure = ingredient['measure'] if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} return shop_list cook_book = get_cook_book() result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2, cook_book) print(result)