最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why the example input gives empty list instead of ['orange juice']? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 8 at 5:27 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Mar 8 at 0:29 Aleph-nullAleph-null 1031 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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
发布评论

评论列表(0)

  1. 暂无评论