/
Denis86
/
File2
Обзор
Документация
Войти
/
Denis86
/
File2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
file 2.py
86 строк
3 KB
Denis86
upload files
18 июн 2025, 23:04
18 июн 2025, 23:04
e6f928e
Код
Авторство
О чём код?
def parse_ingredient_line(line): name, quantity, measure = map(str.strip, line.split('|')) return { 'ingredient_name': name, 'quantity': int(quantity), 'measure': measure } def read_recipes(file_path): cook_book = {} with open('recipes.txt', 'r', encoding='utf-8') as file: while True: dish_name = file.readline().strip() if not dish_name: break ingredient_count = int(file.readline().strip()) ingredients = [] for _ in range(ingredient_count): line = file.readline().strip() if line: ingredients.append(parse_ingredient_line(line)) cook_book[dish_name] = ingredients file.readline() return cook_book def print_cook_book(cook_book): print('{') for dish, ingredients in cook_book.items(): print(f" '{dish}': [") for ing in ingredients: print(f" {ing},") print(" ],") print('}') def get_shop_list_by_dishes(dishes, person_count): cook_book = read_recipes('recipes.txt') shop_list = {} for dish in dishes: if dish not in cook_book: print(f"Блюдо '{dish}' не найдено в кулинарной книге!") continue for ingredient in cook_book[dish]: name = ingredient['ingredient_name'] measure = ingredient['measure'] quantity = ingredient['quantity'] * person_count if name in shop_list: if shop_list[name]['measure'] != measure: print(f"Внимание: для ингредиента '{name}' разные единицы измерения! " f"({shop_list[name]['measure']} vs {measure})") shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} return shop_list def main(): try: cook_book = read_recipes('recipes.txt') print_cook_book(cook_book) dishes = ['Запеченный картофель', 'Омлет'] person_count = 2 print("Список покупок для блюд:", dishes, "на", person_count, "персон:") shop_list = get_shop_list_by_dishes(dishes, person_count) print('{') for ingredient, details in shop_list.items(): print(f" '{ingredient}': {details},") print('}') except FileNotFoundError: print("Ошибка: файл 'recipes.txt' не найден!") except Exception as e: print(f"Произошла ошибка: {e}") if __name__ == '__main__': main()