Python

 

 

 

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

  1. Copy the Python code provided above into a text editor (e.g., VS Code, Sublime Text, or any IDE of your choice).
  2. Save the file with a .py extension, such as recipe_book.py.
  3. Make sure Python is installed on your machine. You can download it from here.
  4. Open a terminal or command prompt and navigate to the directory where your Python file is saved.
  5. Run the program by typing python recipe_book.py in the terminal and pressing Enter.
  6. The program will display all the recipes and allow you to search for a specific recipe by its name.

 

© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)