Program Design and Developement

Share a program that you have expanded upon that was wrtten by another. Show extensive documentation on that program>

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("\nIngredients:")
        for ingredient in self.ingredients:
            print(f"- {ingredient}")
        print("\nInstructions:")
        for instruction in self.instructions:
            print(f"- {instruction}")

def find_recipe(name, recipes):
    for recipe in recipes:
        if recipe.name.lower() == name.lower():
            return recipe
    return None

def delete_recipe(name, recipes):
    for index, recipe in enumerate(recipes):
        if recipe.name.lower() == name.lower():
            del recipes[index]
            return True
    return False

recipes = []

while True:
    print("\nWelcome to the Recipe Book Manager")
    print("Select your action")
    print("A: Add a recipe \nB. View a Recipe \nC. Delete a Recipe \nD. Exit")

    choice = input("Your Choice: ").upper()

    if choice == "A":
        recipe_name = input("Enter the name of the recipe: ")

        ingredients = []
        while True:
            ingredient = input("Add an ingredient (or type 'done' to finish): ")
            if ingredient.lower() == 'done':
                break
            ingredients.append(ingredient)

        instructions = []
        while True:
            instruction = input("Enter the process/instructions for the recipe (Type 'done' to finish): ")
            if instruction.lower() == "done":
                break
            instructions.append(instruction)

        new_recipe = Recipe(recipe_name, ingredients, instructions)
        recipes.append(new_recipe)
        print("Recipe added successfully!")
        new_recipe.display()

    elif choice == "B":
        recipe_name = input("Enter the name of the recipe you want to view: ")
        recipe = find_recipe(recipe_name, recipes)
        if recipe:
            recipe.display()
        else:
            print("Recipe not found!")

    elif choice == "C":
        action = input("Would you like to Edit (E) or Delete (D) a recipe? ").upper()

        if action == "D":
            recipe_name = input("Enter the name of the recipe you want to delete: ")
            if delete_recipe(recipe_name, recipes):
                print("Recipe deleted successfully!")
            else:
                print("Recipe not found!")

    elif choice == "D":
        print("Goodbye!")
        break

Welcome to the Recipe Book Manager
Select your action
A: Add a recipe 
B. View a Recipe 
C. Delete a Recipe 
D. Exit
Recipe added successfully!
Recipe Name: Apple Pie

Ingredients:
- Apple 

Instructions:
- Make Apple Pie

Welcome to the Recipe Book Manager
Select your action
A: Add a recipe 
B. View a Recipe 
C. Delete a Recipe 
D. Exit
Goodbye!

Create a visual representation of this code This was done using a flowchart

Documentation of the program with list and iteration (Using ChatGPT)

"""
Recipe Book Manager
Description: This program allows users to manage a recipe book. Users can add, view, and delete recipes.

Classes:
- Recipe: Represents a single recipe with a name, list of ingredients, and list of instructions.
  - __init__: Initializes a Recipe object with a name, ingredients, and instructions.
  - display: Displays the details of a recipe, including its name, ingredients, and instructions.

Functions:
- find_recipe(name, recipes): Searches for a recipe by name in a list of recipes.
  - Args:
    - name (str): The name of the recipe to find.
    - recipes (list): A list of Recipe objects.
  - Returns:
    - Recipe or None: The found recipe or None if not found.

- delete_recipe(name, recipes): Deletes a recipe by name from a list of recipes.
  - Args:
    - name (str): The name of the recipe to delete.
    - recipes (list): A list of Recipe objects.
  - Returns:
    - bool: True if the recipe was deleted, False if not found.

Variables:
- recipes (list): A list to store Recipe objects.

Main Loop:
- The program starts with a menu-driven interface that allows users to:
  - Add a new recipe with a name, ingredients, and instructions.
  - View the details of a recipe by providing its name.
  - Delete a recipe by providing its name.
  - Exit the program.

- Lists and iterations are used extensively to manage and display recipes.

- While loops are used to gather ingredients and instructions for a new recipe.
"""

# Class to represent a Recipe
class Recipe:
    def __init__(self, name, ingredients, instructions):
        """
        Initialize a Recipe object.

        Args:
            name (str): The name of the recipe.
            ingredients (list): A list of ingredients.
            instructions (list): A list of cooking instructions.
        """
        self.name = name
        self.ingredients = ingredients
        self.instructions = instructions

    def display(self):
        """
        Display the recipe's details, including name, ingredients, and instructions.
        """
        print(f"Recipe Name: {self.name}")
        print("\nIngredients:")
        for ingredient in self.ingredients:
            print(f"- {ingredient}")
        print("\nInstructions:")
        for instruction in self.instructions:
            print(f"- {instruction}")

# Function to find a recipe by name
def find_recipe(name, recipes):
    """
    Find a recipe by its name in the list of recipes.

    Args:
        name (str): The name of the recipe to find.
        recipes (list): A list of Recipe objects.

    Returns:
        Recipe or None: The found recipe or None if not found.
    """
    for recipe in recipes:
        if recipe.name.lower() == name.lower():
            return recipe
    return None

# Function to delete a recipe by name
def delete_recipe(name, recipes):
    """
    Delete a recipe by its name from the list of recipes.

    Args:
        name (str): The name of the recipe to delete.
        recipes (list): A list of Recipe objects.

    Returns:
        bool: True if the recipe was deleted, False if not found.
    """
    for index, recipe in enumerate(recipes):
        if recipe.name.lower() == name.lower():
            del recipes[index]
            return True
    return False

# List to store Recipe objects
recipes = []

# Main program loop
while True:
    print("\nWelcome to the Recipe Book Manager")
    print("Select your action")
    print("A: Add a recipe \nB. View a Recipe \nC. Delete a Recipe \nD. Exit")

    choice = input("Your Choice: ").upper()

    if choice == "A":
        recipe_name = input("Enter the name of the recipe: ")

        ingredients = []
        while True:
            ingredient = input("Add an ingredient (or type 'done' to finish): ")
            if ingredient.lower() == 'done':
                break
            ingredients.append(ingredient)

        instructions = []
        while True:
            instruction = input("Enter the process/instructions for the recipe (Type 'done' to finish): ")
            if instruction.lower() == "done":
                break
            instructions.append(instruction)

        new_recipe = Recipe(recipe_name, ingredients, instructions)
        recipes.append(new_recipe)
        print("Recipe added successfully!")
        new_recipe.display()

    elif choice == "B":
        recipe_name = input("Enter the name of the recipe you want to view: ")
        recipe = find_recipe(recipe_name, recipes)
        if recipe:
            recipe.display()
        else:
            print("Recipe not found!")

    elif choice == "C":
        action = input("Would you like to Edit (E) or Delete (D) a recipe? ").upper()

        if action == "D":
            recipe_name = input("Enter the name of the recipe you want to delete: ")
            if delete_recipe(recipe_name, recipes):
                print("Recipe deleted successfully!")
            else:
                print("Recipe not found!")

    elif choice == "D":
        print("Goodbye!")
        break

Welcome to the Recipe Book Manager
Select your action
A: Add a recipe 
B. View a Recipe 
C. Delete a Recipe 
D. Exit
Recipe added successfully!
Recipe Name: Apple Ice Cream

Ingredients:
- Apple 
- Ice Cream

Instructions:
- make apple icecream

Welcome to the Recipe Book Manager
Select your action
A: Add a recipe 
B. View a Recipe 
C. Delete a Recipe 
D. Exit
Goodbye!

Program with comments that uses a mathematical function


length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))


area = length * width

print(f"The area of the rectangle is: {area}")

The area of the rectangle is: 460.0

Comments

This is a simple Python program that calculates the area of a rectangle based on user input for its length and width. The program follows these key steps:

It prompts the user to input the length and width of the rectangle. The program calculates the area using the formula: Area = length * width. Finally, it displays the calculated area to the user.

<h3> Identifying and Correcting Errors </h3>
def rectangle_area_calculator(length, width):
    area = length * width
    return area

def test_rectangle_area_calculator():
    result = rectangle_area_calculator(5, 3)
    expected = 15
    
    assert result == expected, f"Expected {expected}, but got {result}"

    result = rectangle_area_calculator(0, 10)
    expected = 0

    assert result == expected, f"Expected {expected}, but got {result}"

    result = rectangle_area_calculator(7.5, 2.5)
    expected = 18.75

    assert result == expected, f"Expected {expected}, but got {result}"

if __name__ == "__main__":
    test_rectangle_area_calculator()
    print("All tests passed.")

All tests passed.

Cooments on how If/Else functions better the code

age = int(input("Enter your age: "))

if age > 0:
    
    if age < 18:
        print("You are a minor.")
    else:
        print("You are an adult.")
        
        if age >= 65:
            print("You are a senior citizen.")
else:
    print("Invalid age. Please enter a positive integer for your age.")

You are a minor.

This is a simple python program the uses if and else comments that help reduce or mitigate the erros faced in the results when compared to not using if and else statements at all.

The program asks the user to input there age if the the entered value if less than 18 but greater than 0 then the results would be a “You are a minor”.

Here the program uses an if else statement, if the input is less than 18 then a minor, else i.e if greater than 18 then you are an adult.

The next way an if/else is used is by confirming if the input age is above 65, ie the person is a senior citizen or not. The code does not accept any negetive integers and prints out a value of “Invalid age. Please enter a positive interger for your age.