Untitled
unknown
plain_text
21 days ago
7.2 kB
5
Indexable
import random # Global data structures: # store meal ideas in a dictionary # each key is a category (breakfast, lunch, dinner, snack) # the value is a list of meal names. meal_ideas = { "breakfast": [], "lunch": [], "dinner": [], "snack": [] } # store the weekly plan in a dictionary where # each key is a day of the week, and the value is # another dictionary mapping category -> meal. weekly_meal_plan = {} DAYS_OF_WEEK = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] MEAL_CATEGORIES = ["breakfast", "lunch", "dinner", "snack"] #allow users to add meal ideas def add_meal_idea(): #Allows user to add a meal idea (breakfast, lunch, dinner, snack). print("\n--- Add a Meal Idea ---") category = input("Enter meal category (breakfast/lunch/dinner/snack): ").strip().lower() # Validate category if category not in MEAL_CATEGORIES: print(f"[Error] '{category}' is not a valid category. Please try again.") return meal_name = input("Enter the meal name: ").strip() if meal_name: meal_ideas[category].append(meal_name) print(f"[Success] Added '{meal_name}' to {category} category.") else: print("[Error] Meal name cannot be empty.") # randomly generate weekly meal plan def generate_weekly_meal_plan(): """ Generates a random meal plan for each day of the week, picking one meal from each category. The plan is stored in the global weekly_meal_plan dict. """ # Ensure we start with an empty plan global weekly_meal_plan weekly_meal_plan = {} # Check if we have at least one meal in each category for cat in MEAL_CATEGORIES: if len(meal_ideas[cat]) == 0: print(f"[Error] No meals found in the '{cat}' category. Cannot generate plan.") return for day in DAYS_OF_WEEK: # For each category, pick a random meal from the available ideas day_plan = {} for cat in MEAL_CATEGORIES: day_plan[cat] = random.choice(meal_ideas[cat]) weekly_meal_plan[day] = day_plan print("\n[Success] Weekly meal plan generated!\n") # display generated meal plan def display_meal_plan(): #Prints the current weekly meal plan in an organized format. if not weekly_meal_plan: print("[Note] No weekly plan has been generated yet.") return print("\n--- Weekly Meal Plan ---") for day in DAYS_OF_WEEK: print(f"\n{day}:") plan_for_day = weekly_meal_plan[day] for category in MEAL_CATEGORIES: print(f" {category.capitalize()}: {plan_for_day[category]}") # allow users to customise days def customize_day_meal(): """ Lets the user pick a day and meal category to change, then select from the stored meal ideas to update the plan. """ if not weekly_meal_plan: print("[Note] No weekly plan has been generated yet.") return day_choice = input("\nEnter the day you want to customize (e.g., 'Monday'): ").strip().capitalize() if day_choice not in DAYS_OF_WEEK: print("[Error] Invalid day. Please try again.") return # Show current plan for that day print(f"\nCurrent plan for {day_choice}:") for cat in MEAL_CATEGORIES: print(f" {cat.capitalize()}: {weekly_meal_plan[day_choice][cat]}") # Let the user pick which categories to update categories_to_change = [] while True: category = input("\nEnter a category to update (breakfast/lunch/dinner/snack) or 'done' to finish: ").strip().lower() if category == "done": break elif category in MEAL_CATEGORIES: if category not in categories_to_change: categories_to_change.append(category) else: print("[Error] Invalid category. Try again.") # Update each chosen category for cat in categories_to_change: print(f"\nAvailable options for {cat} are:") for idx, meal_name in enumerate(meal_ideas[cat], start=1): print(f" {idx}. {meal_name}") choice = input(f"Select a meal number for {cat}, or press Enter to skip: ").strip() if choice.isdigit(): choice_index = int(choice) - 1 if 0 <= choice_index < len(meal_ideas[cat]): selected_meal = meal_ideas[cat][choice_index] weekly_meal_plan[day_choice][cat] = selected_meal print(f"[Success] Updated {cat} to '{selected_meal}'.") else: print("[Error] Invalid meal number. Skipped update.") else: print("[Info] No change made for this category.") print("\n[Success] Day customization complete.") #calculate and display total meals def display_meal_summary(): """ Displays how many of each meal type (breakfast, lunch, dinner, snack) are needed for the entire week, based on the current plan. """ if not weekly_meal_plan: print("[Note] No weekly plan has been generated yet.") return # Count how many total breakfasts, lunches, dinners, and snacks meal_counts = {cat: 0 for cat in MEAL_CATEGORIES} for day in weekly_meal_plan: for cat in MEAL_CATEGORIES: meal_counts[cat] += 1 # +1 meal per category/day print("\n--- Shopping Summary ---") for cat in MEAL_CATEGORIES: print(f"{cat.capitalize()}: {meal_counts[cat]} total") #landing page / main menu def main_menu(): #Displays a text-based menu to the user and calls the corresponding functions. while True: print("\n--- Weekly Meal Planner ---") print("1. Add Meal Idea") print("2. Generate Weekly Meal Plan") print("3. Display Weekly Meal Plan") print("4. Customize a Day's Meal(s)") print("5. Show Shopping Summary (Meal Count)") print("6. Exit") choice = input("Enter your choice (1-6): ").strip() if choice == "1": add_meal_idea() elif choice == "2": generate_weekly_meal_plan() elif choice == "3": display_meal_plan() elif choice == "4": customize_day_meal() elif choice == "5": display_meal_summary() elif choice == "6": print("Exiting program. Goodbye!") break else: print("[Error] Invalid choice. Please try again.") # Entry point for the program if __name__ == "__main__": """ starts by populating some sample meal ideas (optional), alternatively could be started with an empty list and let the user add them. """ meal_ideas["breakfast"].extend(["Oatmeal", "Eggs & Toast", "Smoothie"]) meal_ideas["lunch"].extend(["Grilled Chicken Salad", "Turkey Sandwich", "Veggie Wrap"]) meal_ideas["dinner"].extend(["Spaghetti Bolognese", "Grilled Salmon", "Vegetarian Stir-Fry"]) meal_ideas["snack"].extend(["Fruit Salad", "Yogurt", "Protein Bar"]) main_menu()
Editor is loading...
Leave a Comment