Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my function not being printed entirely?

So I made this function with the help of Stack Overflow users, and here it is..

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"]))

This function works, however, for recipecal(recipes, data) only the solution for the first input in the list is being printed out, like the output is only this…

22295

which is the result of ["Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10", but my code ignores the rest of the inputs from the list.. what should I change here to make it work??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The execution of the return calories is too early because it is inside the loop and thus also finishes the loop thats why the other entries were not processed.

The solution to this is to place it outside the inner loop.
When this is the case, what happens is that the return calories only prints the last entry, which is not what you want.

So the final solution is to create a list that will store the calories for each recipe and return that list.

the edit should be like this

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

OUTPUT

[22295, 690, 405]

If you wanted the total sum of all calories then just use sum(list)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading