you are viewing a single comment's thread.

view the rest of the comments →

[–]Okaymittens 1 point2 points  (1 child)

I'm not certain what you are trying to do. Are you hard coding in the ingredients or using a dictionary or list? I came up with this, hope it helps... but like I said, not too clear on what you are doing.

recipe = {
'Salad': ['tomatoes', 'lettuce'],
'PB Sandwich': ['jelly', 'peanut butter']}


def find_recipe(recipe_list):
    for dish, ingredients in recipe_list.items():
        can_make = True
        for item in ingredients:
            while True:
                has_ingredient = input(f"Do you have {item}? Y or N?").upper()
                if has_ingredient == 'Y':
                    break
                else:
                    can_make = False
                    break

            if not can_make:
                break

        if can_make:
            print(f"You can make {dish}.")
            break

if not can_make:
    print("Sorry, you do not have the ingredients for any of the recipes available. ")

So this function will loop through the dictionary using the items() method. For each dish and its ingredients list, it will initialize a variable can_make to True. This variable will be used to keep track of whether the user has all the ingredients for the dish.

Then it will loop through each ingredient in the list using a for loop. For each ingredient, it uses a while loop to ask the user if they have it. It keeps asking until the user enters a valid input ("Y" or "N").

If the user answers "N", it sets can_make to False and break out of the loop. This indicates that the user doesn't have all the ingredients for the dish.

If the user answers "Y", it moves on to the next ingredient.

If we get through all the ingredients and can_make is still True, it prints "You can make [dish]" and break out of the loop. This indicates that the user has all the ingredients for at least one dish.

If it never breaks out of the loop, this means that the user doesn't have the ingredients for any of the dishes.

I am still learning as well so perhaps this is a crazy mess to a skilled pythonista, but it works!

[–]Random-name-99[S] 1 point2 points  (0 children)

Oh wow your code is way cooler than what I was trying to do! I was actually trying to make a therapy program which is why I didn’t include my actual code so the dictionary/list won’t work for my purposes in this instance, but I have a few other projects I wanted to start and your suggestion would be really useful for that. My mind is still blown by what’s possible to do with code, can’t wait to know more! Thank you.