Introduction
Welcome to our digital recipe book program! This simple yet powerful program allows you to store and manage your recipes using Python. You can add new recipes, display them, and even search for specific recipes based on their names. It’s a great way to keep your favorite dishes organized and easily accessible.
Objective
The main objective of this program is to allow users to create and manage a collection of recipes digitally. The Python code lets you add new recipes with ingredients and instructions, list all recipes, and search for a recipe by name. With this program, you can create your own digital recipe book that’s easy to use and navigate.
Python Code for Digital Recipe Book
# Recipe Book Program in Python class Recipe: def __init__(self, name, ingredients, instructions): self.name = name self.ingredients = ingredients self.instructions = instructions def display(self): print(f"Recipe Name: {self.name}") print(f"Ingredients: {', '.join(self.ingredients)}") print(f"Instructions: {self.instructions}\n") class RecipeBook: def __init__(self): self.recipes = [] def add_recipe(self, recipe): self.recipes.append(recipe) def display_recipes(self): if not self.recipes: print("No recipes in the book.") else: for recipe in self.recipes: recipe.display() def search_recipe(self, name): for recipe in self.recipes: if recipe.name.lower() == name.lower(): recipe.display() return print("Recipe not found.") # Example Usage recipe_book = RecipeBook() # Adding recipes recipe_book.add_recipe(Recipe("Pancakes", ["flour", "eggs", "milk", "butter"], "Mix ingredients and cook on a griddle.")) recipe_book.add_recipe(Recipe("Spaghetti", ["spaghetti", "tomato sauce", "garlic", "olive oil"], "Boil spaghetti and mix with sauce.")) # Display all recipes print("All Recipes:") recipe_book.display_recipes() # Search for a recipe by name print("Search for 'Pancakes':") recipe_book.search_recipe("Pancakes")
Explanation of the Program Structure
The program consists of two main classes: Recipe
and RecipeBook
.
- Recipe class: This class represents a single recipe. It has attributes such as the recipe’s name, ingredients, and instructions. The
display
method prints these details to the console. - RecipeBook class: This class manages a collection of recipes. It has methods to add new recipes to the book (
add_recipe
), display all recipes (display_recipes
), and search for a recipe by its name (search_recipe
).
How to Run the Program
- Copy the Python code provided above into a text editor (e.g., VS Code, Sublime Text, or any IDE of your choice).
- Save the file with a .py extension, such as
recipe_book.py
. - Make sure Python is installed on your machine. You can download it from here.
- Open a terminal or command prompt and navigate to the directory where your Python file is saved.
- Run the program by typing
python recipe_book.py
in the terminal and pressing Enter. - The program will display all the recipes and allow you to search for a specific recipe by its name.