/
MadKing
/
HomeWorkOpenFileTasks
Обзор
Документация
Войти
/
MadKing
/
HomeWorkOpenFileTasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
main.py
49 строк
2 KB
Stas Solovev
Done_HomeWork
16 май 2025, 22:39
16 май 2025, 22:39
0c065cd
Код
Авторство
О чём код?
def load_recipes(): cook_book = {} with open('recipes.txt', encoding='UTF-8') as file: lines = file.readlines() i = 0 while i < len(lines): dish_name = lines[i].strip() i += 2 ingredients = [] while i < len(lines) and lines[i].strip(): ingredient_data = lines[i].strip().split(' | ') ingredient_name = ingredient_data[0] quantity = int(ingredient_data[1]) measure = ingredient_data[2] ingredients.append({ 'ingredient_name': ingredient_name, 'quantity': quantity, 'measure': measure }) i += 1 cook_book[dish_name] = ingredients i += 1 return cook_book def get_shop_list_by_dishes(dishes, person_count): book = load_recipes() shop_list = {} repeat_list = {} repeat = 0 for dish in dishes: for i in book[dish]: if i.get('ingredient_name') in shop_list: repeat_list.update({i.get('ingredient_name'): {'repeat': 0}}) repeat += 1 repeat_list.update({i.get('ingredient_name'): {'repeat': repeat}}) else: shop_list.update ({ i.get('ingredient_name'):{ 'measure': i.get('measure'), 'quantity': i.get('quantity') * person_count } }) if repeat_list: for i in repeat_list: shop_list[i]['quantity'] *= repeat_list[i]['repeat'] for ing in shop_list: print(ing, shop_list.get(ing)) get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'],2)