94.4k views
2 votes
Create a dictionary that will hold AT LEAST 3 categories for food with at least 3 foods for each category. E.g. Fruits --> Apple, Strawberries, Orange; Vegetables --> Carrots, Peas, Onions; etc.. Once you have populated your dictionary with categories AND foods for each category, output the all the categories and the foods that you have for them (e.g. Fruits: Apple, Strawberries, Orange). Ask the user if they would like to add a new category OR a new food. If they say a new category, make sure that it is actually a new category and not an existing one. If it is a food, add it to the list of foods that you already had. Ask the user if they would like to enter something else, and if they say yes, keep going, otherwise don't prompt them to input any more information. Once the user has added all of their information, output all of the categories for food and ask the user to pick one to see all of the foods. You only need to do this once, but feel free to make a loop to let them do this as many times as they want.

User Peter Jack
by
3.4k points

1 Answer

1 vote

Answer:

  1. food_category = {
  2. "Fruits": ["Apple", "Strawberries", "Orange"],
  3. "Vegetables": ["Carrots", "Peas", "Onions"],
  4. "Meats": ["Chicken", "Beef", "Mutton"]
  5. }
  6. for cat in food_category:
  7. output = cat + ": "
  8. for food in food_category[cat]:
  9. output += food + ", "
  10. print(output)
  11. answer = input("Do you want to add category or food (Y - Yes N - No): ")
  12. while(answer == "Y"):
  13. choice = input("New Category (N) or New Food (nf): ")
  14. if(choice == "N"):
  15. new_cat = input("Enter new category name: ")
  16. if(new_cat not in food_category):
  17. food_category[new_cat] = ""
  18. elif(choice == "nf"):
  19. cat = input("Enter category: ")
  20. new_food = input("Enter new food name: ")
  21. food_category[cat].append(new_food)
  22. answer = input("Do you want to add new category or food (Y - Yes N - No): ")
  23. print("All food categories: ")
  24. for cat in food_category:
  25. print(cat)
  26. selected_cat = input("Select a category to view the food list: ")
  27. output = ""
  28. for food in food_category[selected_cat]:
  29. output += food + ", "
  30. print(output)

Step-by-step explanation:

The solution code is written in Python 3.

Firstly, create a food category dictionary data structure (Line 1 -5). Next, display all the food category and the food in each category (Line 7 - 11)

Next prompt user to feedback if they wish to add new category or new food (Line 13).

While the answer is yes prompt user to enter their choice, either category or food and use if else if statements to handle the choice made by the user. If the choice is new category, prompt user to enter new category name and check if the new category is already exist in the dictionary. If not, add that category to dictionary (Line 18 - 21).

If the choice is food, nf, prompt user to input food category and enter the new food name and add it to the food_category list (Line 22 - 25).

After that prompt user to enter if they wish to continue to add new data (Line 27).

After collecting all info from user, write a for loop to display all the categories (Line 29 - 31).

At last, prompt user to select a category and display all the food of the selected category (Line 33 - 37).

User Malado
by
4.0k points