/
A_V_A
/
Homework
Обзор
Документация
Войти
/
A_V_A
/
Homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cook_book.py
59 строк
2 KB
Артём А.
Cook_book_задача1_задача2
23 фев 2026, 14:39
Верифицирован
23 фев 2026, 14:39
ae47902
Код
Авторство
О чём код?
# Задача №1 Должен получится следующий словарь... from pprint import pprint path = r'C:\Users\artem\OneDrive\Рабочий стол\python_work\HW_recipes\recipes.txt' def create_book_recipes(path): cook_book = {} with open(path, 'r', encoding='utf-8') as file: for string in file: dish_name = string.strip() if not dish_name: continue ingredient_count = int(file.readline().strip()) ingredient = [] for i in range(ingredient_count): ingredient_string = file.readline().strip() section = ingredient_string.split(' | ') ingredient_name = section[0] quantity = int(section[1]) measure = section[2] ingredient_dict = { 'ingredient_name': ingredient_name, 'quantity': quantity, 'measure': measure } ingredient.append(ingredient_dict) cook_book[dish_name] = ingredient return cook_book cook_book = create_book_recipes(path) print('\n') pprint(cook_book, sort_dicts=False, width=120) print('\n') # ================================================================================================================= # Задача №2 написать функцию, которая на вход принимает список блюд из cook_book и # количество персон для кого мы будем готовить 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]: all_quantity = ingredient['quantity'] * person_count ingredient_name = ingredient['ingredient_name'] if ingredient_name not in shop_list: shop_list[ingredient_name] = { 'measure': ingredient['measure'], 'quantity': all_quantity } else: shop_list[ingredient_name]['quantity'] += all_quantity else: print(f"Блюда {dish} нет в cook_book") return shop_list result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2) print('проверка задачи №2') pprint (result)