Question: 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

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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!