SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Simple Functions
Exercise:
Programming Exercises
Question:7 | ISBN:9780132576376 | Edition: 2

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.