pythondebugging

Why the example input gives empty list instead of ['orange juice']?


Problem Statement

Safet decided that he will adopt a healthy lifestyle. He carefully inspects the nutritional content of the food he buys and makes sure that the food he eats contains the right amount of protein, carbohydrates, and fats. He has a list of food items and their nutritional content. He stored this information in the form of a dictionary (see below).

food_example = {
    'chocolate bar': {'sodium': 50, 'cholesterol': 10, 'saturated_fat': 20, 'trans_fat': 0.1,
                      'dietary_fiber': 6, 'sugar': 45, 'protein': 7, 'calcium': 100, 'iron': 3},
    'yogurt': {'sodium': 46, 'cholesterol': 13, 'saturated_fat': 2.1, 'trans_fat': 0,
                      'dietary_fiber': 0, 'sugar': 4.7, 'protein': 3.5, 'calcium': 121, 'potassium': 155,
                      'B12': 0.4, 'phosphorus': 135},
    'orange juice': {'sodium': 1, 'cholesterol': 0, 'saturated_fat': 0, 'trans_fat': 0,
                      'dietary_fiber': 0.2, 'sugar': 8.4, 'protein': 0.7, 'calcium': 11, 'potassium': 200,
                      'C': 50, 'folate': 30},
}

Safet wants to avoid certain nutrients and he wants to consume sufficient amounts of other nutrients. Write a function perfect_food(food_specs, avoid, need) that takes the food specification (as above), a list of nutrients to avoid, and a dictionary of nutrients and their minimal amounts. The function should return a list of food items that Safet could eat in order to avoid the nutrients in the avoid list and to consume at least the minimal amounts of the nutrients in the need dictionary. Example:

>>> perfect_food(food_example, ['saturated_fat'], {'potassium': 100})
 ['orange juice']

......................................................................................

My Code

def perfect_food(food_specs, avoid, need):
    suitable_foods = list(food_specs.keys())
    for food, nutrients in food_specs.items():
        for nutrient in need:
            if nutrient not in nutrients:
                suitable_foods.remove(food)
                break
        if food in suitable_foods:
            for element, value in nutrients.items():
                if element in avoid:
                    suitable_foods.remove(food)
                    break
                if element in need and value < need[element]:
                        suitable_foods.remove(food)
                        break
    return suitable_foods

Why my code above is giving [] instead of ['orange juice'] for perfect_food(food_example, ['saturated_fat'], {'potassium': 100})? How can I fix it?


Solution

  • Step through the code in a debugger and you will find that orange juice is removed because it contains a saturated_fat key. Adjust the code to handle the case that the key is there but the value is zero.

            if food in suitable_foods:
                for element, value in nutrients.items():
                    if element in avoid and value > 0:
    #                                  ^^^^^^^^^^^^^^  add this