Project Description¶
We are joining a Health and Leisure company to add an important new feature to their app.
In this project, we will use our Python skills to create a function that calculates calories and nutritional values based on user input. Our goal is to help customers make better dietary choices and improve their health.
Our task is to build a calorie and nutrition calculator. This tool will compute and display total calories, sugars, fats, and other nutrition details for different foods based on what the user enters.
We have a dataset named nutrition.json
that contains nutritional information for many foods. Each value in this dataset represents the amount per 100 grams of the food.
Dataset Summary¶
nutrition.json
Column | Description |
---|---|
food |
The name of the food. |
calories |
Energy provided by the food, measured in kilocalories (kcal) per 100 grams. |
total_fat |
Total fat content in grams per 100 grams. |
protein |
Protein content in grams per 100 grams. |
carbohydrate |
Total carbohydrate content in grams per 100 grams. |
sugars |
Amount of sugars in grams per 100 grams. |
Let's Get Started!¶
This project gives us a chance to build a real-world feature from scratch. It will demonstrate our programming skills while creating a useful tool that helps users improve their health and wellness. lth and wellness.
Enhancing the Diet Coach app by creating the nutritional_summary() function to calculate and return the total nutritional values.¶
# Import the json module
import json
# 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]
[('Cornstarch', {'calories': 381, 'total_fat': 0.1, 'protein': 0.26, 'carbohydrate': 91.27, 'sugars': 0.0}), ('Nuts, pecans', {'calories': 691, 'total_fat': 72.0, 'protein': 9.17, 'carbohydrate': 13.86, 'sugars': 3.97}), ('Eggplant, raw', {'calories': 25, 'total_fat': 0.2, 'protein': 0.98, 'carbohydrate': 5.88, 'sugars': 3.53})]
# Define a function to calculate the nutritional summary
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}))
{'calories': 733.5, 'total_fat': 32.0, 'protein': 15.55, 'carbohydrate': 96.5, 'sugars': 38.025} Croissant