Recipe Book using Python Programming!!

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

import json


class Recipe:
def __init__(self, name, ingredients, instructions):
self.name = name
self.ingredients = ingredients
self.instructions = instructions

def __repr__(self):
return f"Recipe({self.name}, {self.ingredients}, {self.instructions})"


class RecipeBook:
def __init__(self, filename="recipes.json"):
self.recipes = []
self.filename = filename
self.load_recipes()

def add_recipe(self, recipe):
self.recipes.append(recipe)
self.save_recipes()

def display_recipes(self):
for recipe in self.recipes:
print(f"Name: {recipe.name}")
print("Ingredients:")
for ingredient in recipe.ingredients:
print(f"- {ingredient}")
print("Instructions:")
print(recipe.instructions)
print("\n")

def save_recipes(self):
with open(self.filename, 'w') as file:
json.dump([recipe.__dict__ for recipe in self.recipes], file)

def load_recipes(self):
try:
with open(self.filename, 'r') as file:
recipes = json.load(file)
for recipe in recipes:

except FileNotFoundError:
pass


# Usage
recipe_book = RecipeBook()

# Adding a new recipe
new_recipe = Recipe(
name="Pancakes",
ingredients=["2 cups all-purpose flour", "2 tablespoons sugar", "2 teaspoons baking powder", "1/2 teaspoon salt",
"2 eggs", "1 1/2 cups milk", "1/4 cup melted butter"],
instructions="1. Mix dry ingredients. 2. Add wet ingredients. 3. Cook on a griddle."
)


# Display all recipes
recipe_book.display_recipes()

programmer-designer
join shbcf.ru