Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(0) import os import csv os.chdir('/Users/sadsdasd/Desktop/CS/Class/CS162/Week4') def open_file(): Function to open the input file file_name = input(Please enter the file name: ) if os.path.isfile(file_name): return

(0)

import os

import csv

os.chdir('/Users/sadsdasd/Desktop/CS/Class/CS162/Week4')

def open_file():

"""Function to open the input file"""

file_name = input("Please enter the file name: ")

if os.path.isfile(file_name):

return file_name

else:

print("File not found. Please try again.")

return open_file()

def build_dictionary(filename: os.PathLike):

recipes = {}

with open(filename, "r") as file:

reader = csv.DictReader(file)

for row in reader:

prep_time = int(row['prep_time'])

cook_time = int(row['cook_time'])

recipes[row['name']] = {

'ingredients': row['ingredients'].split(", "),

'diet': row['diet'],

'prep_time': prep_time,

'cook_time': cook_time,

'flavor_profile': row['flavor_profile'],

'course': row['course'],

'state': row['state'],

'region': row['region']

}

return recipes

def get_ingredients(recipes, food):

ingredients = set()

for name, recipe in recipes.items():

if name == food:

ingredients.update(recipe['ingredients'])

return ingredients

def get_useful_and_missing_ingredients(recipes, pantry):

useful_ingredients = set()

missing_ingredients = set()

for food in recipes:

food_ingredients = get_ingredients(recipes, food)

for ingredient in food_ingredients:

if ingredient in pantry:

useful_ingredients.add(ingredient)

else:

missing_ingredients.add(ingredient)

return useful_ingredients, missing_ingredients

def get_list_of_foods(recipes, ingredients):

foods = []

for recipe in recipes.values():

if all(ingredient in recipe['ingredients'] for ingredient in ingredients):

foods.append(recipe['name'])

return foods

def get_food_by_preference(recipes: dict, preference: str) -> list:

foods = []

for food, recipe in recipes.items():

if recipe['flavor_profile'] == preference:

foods.append(food)

return foods

def main():

print("Indian Foods & Ingredients. ")

file_name = open_file()

recipes = build_dictionary(file_name)

MENU = """

A. Input various foods and get the ingredients needed to make them!

B. Input various ingredients and get all the foods you can make with them!

C. Input various foods and ingredients and get the useful and missing ingredients!

D. Input various foods and preferences and get only the foods specified by your preference!

Q. Quit

: """

while True:

print(MENU)

choice = input("Enter your choice [A-D, Q]: ").upper()

if choice == "A":

food = input("Enter food name: ")

ingredients = get_ingredients(recipes, food)

print("Ingredients:", ingredients)

elif choice == "B":

ingredients = input("Enter ingredients separated by commas: ").strip().split(",")

foods = get_list_of_foods(recipes, ingredients)

print("Foods:", foods)

elif choice == "C":

pantry = input("Enter ingredients separated by commas: ").strip().split(",")

useful, missing = get_useful_and_missing_ingredients(recipes,ingredients, pantry)

print("Useful ingredients:", useful)

print("Missing ingredients:", missing)

elif choice == "D":

preference = input("Enter food preference: ")

foods = get_food_by_preference(recipes, preference)

print("Foods:", foods)

elif choice == "Q":

break

else:

print("Invalid choice. Try again.")

print("Thanks for Playing!")

if __name__ == "__main__":

main()

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Fix this code. It doesn't work properly and I don't know why.

Here is the csv file I'm using:

image text in transcribed

name, ingredients, diet, prep_time, cook_time, flavor_profile, course, state, region Balu shahi, "Maida flour, yogurt, oil, sugar", vegetarian,45,25, sweet, dessert, West Bengal, East Boondi, "Gram flour, ghee, sugar", vegetarian, 80, 30, sweet, dessert, Rajasthan, West Gajar ka halwa, "Carrots, milk, sugar, ghee, cashews, raisins", vegetarian, 15, 60, sweet, dessert, Punjab, North Ghevar, "Flour, ghee, kewra, milk, clarified butter, sugar, almonds, pistachio, saffron, green cardamom", vegetar Chikki, "Peanuts, jaggery", vegetarian, 10, 20, sweet, dessert, Maharashtra, West Dharwad pedha, "Milk, Sugar, Dharwadi buffalo milk", vegetarian, 20, 60, sweet, dessert, Karnataka, South Kajjikaya, "Rice flour, jaggery, coconut", vegetarian, 40, 15, sweet, dessert, Andhra Pradesh, South Anarsa, "Rice flour, jaggery, khus-khus seeds", vegetarian, 10, 50, sweet, dessert, Maharashtra, West Basundi, "Sugar, milk, nuts", vegetarian, 10, 35, sweet, dessert, Gujarat, West Lassi, "Yogurt, milk, nuts, sugar", vegetarian, 5, 5, sweet, dessert, Punjab, North Kheer, "Milk, rice, sugar, dried fruits", vegetarian, 10,40 , sweet, dessert, 1,1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL 10 High Performance Expert Techniques For Query Optimization High Availability And Efficient Database Maintenance

Authors: Ibrar Ahmed ,Gregory Smith ,Enrico Pirozzi

3rd Edition

1788474481, 978-1788474481

More Books

Students also viewed these Databases questions