如何修复有助于根据食谱准备菜肴的功能?

2024-06-01 03:15:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试执行一个函数,只要用户在他们的库存中有足够的原料,就可以准备一道菜。但是,我很不确定如何创建这个函数。你知道吗

到目前为止,我有:

def CanPrepareDish(recipe_name):
    recipe = cookbook.getRecipe(recipe_name)
    for ingred in recipe:
        if ingred not in iinventory:
            return False
        if iinventory[ingred] < recipe[ingred]:
            return False
    return True

def preparedish(recipe_name):
    if not CanPrepareDish(recipe_name):
        print("Not enough ingredients")
    else:
        dish = cookbook.getRecipe(recipe_name)
        recipe = dish.getName()
        for recipe in cookbook.allRecipes():
            iinventory[ingred] -= recipe[ingred]
        if recipe_name == recipe.getName():
            iinventory[recipe_name] += 1
        else:
            iinventory[recipe_name] = 1
            print("Dish prepared")


class Cookbook:
    def __init__(self):
        self.Cooklist=[]
    def addRecipe(self,Recipe):
        self.Cooklist.append(Recipe)
    def getRecipe(self,recipe_name):
        for recipe in self.Cooklist:
            if recipe_name == recipe.getName():
                return recipe
    def allRecipes(self):
        for Recipe in self.Cooklist:
            return self.Cooklist

cookbook=Cookbook()

class Iinventory: def __init__(self): self.apple_num=0 self.beet_num=0 self.carrot_num=0 def getApples(self): return int(self.apple_num) def getBeets(self): return int(self.beet_num) def getCarrots(self): return int(self.carrot_num) iinventory=Iinventory()

CanPrepareDish(recipe_name)我想检查一下是否有足够的原料让他们真正准备这道菜,所以如果他们不能preparedish就会打印出他们没有足够的原料。但是我不能遍历配方,因为它是一个对象,所以我如何遍历配方来查看是否有足够的成分?你知道吗

有了preparedish,我不知道该怎么做。因此,它应该检查是否有足够的配料与CanPrepareDish,如果可以,然后它继续准备菜和删除使用的配料从iinventory(这也是一个对象)。你知道吗

创建的配方对象/类。你知道吗

class Recipe: def __init__(self,name,apple_num,beet_num,carrot_num): self.name=str(name) self.apple_num=int(apple_num) self.beet_num=int(beet_num) self.carrot_num=int(carrot_num) def getName(self): return self.name def getApples(self): return self.apple_num def getBeets(self): return self.beet_num def getCarrots(self): return self.carrot_num

Tags: nameinselfapplereturnifdefrecipe
1条回答
网友
1楼 · 发布于 2024-06-01 03:15:20

原来的答案如下,这个顶部部分是一个更新,以反映新的信息。首先,请参见下面的代码转储:

class Ingredients:
    def __init__(self):
        self.ingredients = {"apples": 0, "beets": 0, "carrots": 0}

    def getApples(self):
        return self.ingredients["apples"]

    def getBeets(self):
        return self.ingredients["beets"]

    def getCarrots(self):
        return self.ingredients["carrots"]


class Iinventory(Ingredients):
    def __init__(self):
        Ingredients.__init__(self)


class Recipe(Ingredients):
    def __init__(self, name, apple_num, beet_num, carrot_num):
        Ingredients.__init__(self)
        self.name = name
        self.ingredients["apples"] = apple_num
        self.ingredients["beets"] = beet_num
        self.ingredients["carrots"] = carrot_num

    def getName(self):
        return self.name


def CanPrepareDish(recipe_name, inventory):
    recipe = cookbook.getRecipe(recipe_name)
    for ingred in recipe.ingredients.keys():
        if ingred not in inventory.ingredients.keys():
            return False
        if inventory.ingredients[ingred] < recipe.ingredients[ingred]:
            return False
    return True


def preparedish(recipe_name, inventory):
    if not CanPrepareDish(recipe_name, inventory):
        print("Not enough ingredients")
    else:
        dish = cookbook.getRecipe(recipe_name)
        recipe = dish.getName()
        for recipe in cookbook.allRecipes():
            inventory[ingred] -= recipe[ingred]
        if recipe_name == recipe.getName():
            inventory[recipe_name] += 1
        else:
            inventory[recipe_name] = 1
            print("Dish prepared")


class Cookbook:
    def __init__(self):
        self.Cooklist = []

    def addRecipe(self, Recipe):
        self.Cooklist.append(Recipe)

    def getRecipe(self, recipe_name):
        for recipe in self.Cooklist:
            if recipe_name == recipe.getName():
                return recipe

    def allRecipes(self):
        for _ in self.Cooklist:
            return self.Cooklist


cookbook = Cookbook()
r = Recipe("food", 1, 2, 3)
cookbook.addRecipe(r)
iinventory = Iinventory()
iinventory.ingredients["apples"] = 5
iinventory.ingredients["beets"] = 5
iinventory.ingredients["carrots"] = 5

print(iinventory.getApples())
print(r.getBeets())

if CanPrepareDish("food", iinventory):
    print("Can make recipe")
else:
    print("Nope")

在我解释这些变化之前,我只想指出几个小问题。1preparedish函数现在不起作用,因为它使用了一个来自函数外部的ingred变量-我假设您可以控制它。2如果不在库存.配料.钥匙()部分永远不会被触发,因为按照目前的结构,inventory和Recipe类都将始终在其中定义所有的成分。你知道吗

那么,来解释一下上面的情况。首先,我注意到inventory和Recipe类复制了很多相同的东西,所以我将它们都创建为顶级配料类的子类。事实上,目前的存货只是一个新名称下的配料。有一点更多的食谱,但基本上,它只是设置与以前一样的东西。严格地说,你实际上不需要在配料的init定义全零字典,但我个人觉得它更整洁。你知道吗

要对不同类型的成分进行迭代,只需调用 每个对象的成员。你知道吗

另外,出于某些原因,我让CanPrepareDish和preparedish函数将要使用的库存作为另一个参数。这意味着我可以在顶部定义这些东西,然后在底部处理它们,这完全取决于你是否保持这种方式。你知道吗

我在底部包含了一些测试实现的代码。如果注释掉preparedish()函数,您应该可以看到它全部工作。你知道吗

你有什么需要进一步澄清的地方吗?你知道吗


正如@Denziloe所指出的,目前你有三个独立的变量,一般来说,你不能遍历独立的变量。你知道吗

根据你在CanPrepareDish中所掌握的知识,我认为你最好的方法可能是使用字典来存储配料。请注意,我不能确定这是你想要的,因为我不完全确定什么是库存。你知道吗

因此,首先,将Recipe类更改为使用字典而不是单个变量:

class Recipe:
    def __init__(self, name, apple_num, beet_num, carrot_num):
        self.name = str(name)
        self.ingredientsList = {}
        self.ingredientsList["apples"] = apple_num
        self.ingredientsList["beets"] = beet_num
        self.ingredientsList["carrots"] = carrot_num

    def getName(self):
        return self.name

    def getApples(self):
        return self.ingredientsList["apples"]

    def getBeets(self):
        return self.ingredientsList["beets"]

    def getCarrots(self):
        return self.ingredientsList["carrots"]

然后将CanPrepareDish更改为:

def CanPrepareDish(recipe_name):
    recipe = cookbook.getRecipe(recipe_name)
    for ingred in recipe.ingredientsList.keys():
        if ingred not in iinventory:
            return False
        if iinventory[ingred] < recipe.ingredientsList[ingred]:
            return False
    return True

对CanPrepareDish函数的唯一更改是将for循环更改为userecipe.ingredientsList.keys配方(). 你知道吗

试试看它是否有用。否则,您可能需要再次编辑问题,向我们展示iinventory的外观,以便任何人都能够进一步提供帮助。你知道吗

相关问题 更多 >