Calories from Fat and Carbohydrates
A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day. Then, she calculates the number of calories that result from
the fat, using the following formula:
calories from fat = fat grams x 9
Next, she calculates the number of calories that result from the carbohydrates, using the following formula:
calories from carbs = carb grams x 4
The nutritionist asks you to write a program that will make these calculations.
Calories from Fat and Carbohydrates python code:
#The method "calculate_calories_from_fat" is used
#to calculates the number of calories
#that result from the fat
def calculate_calories_from_fat(fat_grams):
calories_from_fat = fat_grams * 9
return calories_from_fat
#The method "calculate_calories_from_carbs" is used
#to calculates the number of carbs
def calculate_calories_from_carbs(carb_grams):
calories_from_carbs = carb_grams * 4
return calories_from_carbs
#main method:prompt and asks members for
#the number of fat grams and carbohydrate grams
def main():
fat_grams = float(input("Enter the number of fat grams consumed: "))
carb_grams = float(input("Enter the number of carbohydrate grams consumed: "))
#Call the methods
calories_from_fat = calculate_calories_from_fat(fat_grams)
calories_from_carbs = calculate_calories_from_carbs(carb_grams)
#Display output
print("Calories from fat:", calories_from_fat)
print("Calories from carbohydrates:", calories_from_carbs)
if __name__ == "__main__":
main()
Executed Output:
Enter the number of fat grams consumed: 223
Enter the number of carbohydrate grams consumed: 3
Calories from fat: 2007.0
Calories from carbohydrates: 12.0