Skip to content

As a Software Engineer in a Health and Leisure company, your task is to add a new feature to the app: a calorie and nutrition calculator. This tool will calculate and display total calories, sugars, fats, and other nutritional values for different foods based on user input.

You have been provided with the nutrition.json dataset, which contains the necessary nutritional information for various foods. Each value in the dataset is per 100 grams of the food item. The dataset has already been read and loaded for you as the dictionary nutrition_dict.

Dataset Summary

nutrition.json

ColumnDescription
foodThe name of the food.
caloriesThe amount of energy provided by the food, measured in kilocalories (kcal) per 100 grams.
total_fatThe total fat content in grams per 100 grams.
proteinThe protein content in grams per 100 grams.
carbohydrateThe total carbohydrate content in grams per 100 grams.
sugarsThe amount of sugars in grams per 100 grams.

Let's Get Started!

This project is a great opportunity to build a real-world feature from scratch, showcasing your development skills and making a meaningful impact on users' health and wellness.

import json  # Import the json module to work with JSON files

# Open the nutrition.json file in read mode and load its content into a dictionary
with open('nutrition.json', 'r') as json_file:
    nutrition_dict = json.load(json_file)  # Load the JSON content into a dictionary
    
# Display the first 3 items of the nutrition dictionary
list(nutrition_dict.items())[:3]
# Start coding here!
# Use as many cells as you need.

import json  # Import the json module to work with JSON files

# Open the nutrition.json file in read mode and load its content into a dictionary
with open('nutrition.json', 'r') as json_file:
    nutrition_dict = json.load(json_file)  # Load the JSON content into a dictionary
    
# Display the first 3 items of the nutrition dictionary
list(nutrition_dict.items())[:3]

# Define a function to calculate the nutritional summary given a dictionary of foods and their weights
def nutritional_summary(foods):
    # Initialize result dictionary to store total nutritional values
    result_dict = {"calories": 0, "total_fat": 0, "protein": 0, "carbohydrate": 0, "sugars": 0} 
    
    # Process each food item
    for name, grams in foods.items():
        if name in nutrition_dict:
            # Get the nutritional information for the food item
            nutrition = nutrition_dict[name]
            # Calculate and add the nutritional values based on the given weight (grams)
            result_dict["calories"] += grams * nutrition["calories"] / 100
            result_dict["total_fat"] += grams * nutrition["total_fat"] / 100
            result_dict["protein"] += grams * nutrition["protein"] / 100
            result_dict["carbohydrate"] += grams * nutrition["carbohydrate"] / 100
            result_dict["sugars"] += grams * nutrition["sugars"] / 100
        else:
            # Return the name of the first food item not found in the nutrition_dict
            return name
    # Return the total nutritional values
    return result_dict

# Calling the function and checking the output
print(nutritional_summary({"Croissants, cheese": 150, "Orange juice, raw": 250}))
print(nutritional_summary({"Croissant": 150, "Orange juice": 250}))