如果您曾经玩过我的世界,以下内容会更有意义。由于你们中的许多人没有,我会尽力解释它
我正在尝试编写一个递归函数,该函数可以找到从我的世界食谱平面文件中制作任何我的世界物品的步骤。这个我真的难住了。
平面文件有点长,所以我将它包含在这个要点中。
def getRecipeChain(item, quantity=1):
#magic recursive stuffs go here
所以基本上我需要查找第一个食谱,然后查找第一个食谱的所有组件的食谱,依此类推,直到你找到没有食谱的项目。每次我需要将食谱附加到一个列表中,这样我就会得到一个关于制作物品顺序的指令集。
所以这是我现在拥有的功能(那个不起作用)
def getRecipeChain(name, quantity=1):
chain = []
def getRecipe(name1, quantity1=1):
if name1 in recipes:
for item in recipes[name1]["ingredients"]["input"]:
if item in recipes:
getRecipe(item, quantity1)
else:
chain.append(item)
getRecipe(name, quantity)
return chain
这是我想要的理想输出。它是一个字典,其中存储了项目名称和数量。
>>> getRecipeChain("solar_panel", 1):
{"insulated_copper_cable":13, "electronic_circuit":2, "re_battery":1, "furnace":1, "machine":1, "generator":1, "solar_panel":1}
所以问题是,我该怎么做?
我知道在这里要求人们为你工作是不受欢迎的,所以如果你觉得这有点太接近你只是为我做编码,那就这么说吧。