【问题标题】:Why is my function not being printed entirely?为什么我的函数没有完全打印出来?
【发布时间】:2021-12-31 06:19:42
【问题描述】:

所以我在 Stack Overflow 用户的帮助下做了这个功能,就在这里..

def totalcal(data):
    calorie_dict = {}
    for el in data:
      food, values = el.split(':')
      a, b, c = values.split(',')
      calories = (int(a) * 5) + (int(b) * 5) + (int(c) * 9)
      calorie_dict[food] = calories
    return calorie_dict


 def rdict(recipes):
        recipes_splitted = {}
        for r in recipes:
            recipe_name, parts = r.split(":")
            recipe_parts = {}
            for part in parts.split(','):
                product, number = part.split('*')
                recipe_parts[product] = int(number)
            recipes_splitted[recipe_name] = recipe_parts
        return recipes_splitted



 def recipecal(recipes, data):
        for recipe_name in rdict(recipes):
            recipe_parts = rdict(recipes)[recipe_name]
            calories = 0
            for product in recipe_parts:
                number = recipe_parts[product]
                calories += number * totalcal(data)[product]
            return calories

print(recipecal(["Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
    "Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
    "T-Bone:Carrot*2,Steak Meat*1"]
    ,["Cabbage:4,2,0", "Carrot:9,1,5", "Fatty Pork:431,1,5",
    "Pineapple:7,1,0", "Steak Meat:5,20,10", "Rabbit Meat:7,2,20"]))

但是,对于recipecal(recipes, data),此函数有效,只有列表中第一个输入的解决方案被打印出来,就像输出只是这个......

22295

这是["Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10", 的结果,但我的代码忽略了列表中的其余输入。我应该在此处更改什么以使其正常工作??

【问题讨论】:

  • 我现在不知道它是否是格式错误,但您的 return calories 语句在循环内。所以,它只执行一次!
  • 基本调试是你自己的责任。你需要找到代码偏离你期望的地方

标签: python list dictionary tuples


【解决方案1】:

return calories 的执行太早了,因为它在循环内,因此也完成了循环,这就是其他条目未被处理的原因。

解决方案是将其置于内部循环之外。 在这种情况下,返回的卡路里只会打印最后一个条目,这不是您想要的。

所以最终的解决方案是创建一个列表来存储每个食谱的卡路里并返回该列表。

编辑应该是这样的

def recipecal(recipes, data):
        calories_list = []
        for recipe_name in rdict(recipes):
            recipe_parts = rdict(recipes)[recipe_name]
            calories = 0
            for product in recipe_parts:
                number = recipe_parts[product]
                calories += number * totalcal(data)[product]
            calories_list.append(calories)
        return calories_list

输出

[22295, 690, 405]

如果您想要所有卡路里的总和,那么只需使用sum(list)

【讨论】:

    【解决方案2】:

    for recipe_name in rdict(recipes): 循环中有一个 return 语句,所以它会在循环之前返回,并且永远不会到达其他元素。通过将 calories = 0 和 return calories 移出循环来修复它。

    def recipecal(recipes, data):
        calories = 0
        for recipe_name in rdict(recipes):
            recipe_parts = rdict(recipes)[recipe_name]
            for product in recipe_parts:
                number = recipe_parts[product]
                calories += number * totalcal(data)[product]
        return calories
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多