In: Computer Science
Write a Python program that uses function(s) for the following problem:
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 (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:
\(calories\:from\:fat\:=\:fat\:grams\:\times9\)
Next, she calculates the number of calories that result from the carbohydrates, using the following formula:
caloriesfromcabs=carbgrams×4
The nutritionist asks you to write a program that will make these calculations. Make sure that your program does not allow user to enter negative values for fat grams and carb grams (that is, validate the input with a loop).
Note:
you can write one function that takes as its parameters the number of grams and calories generated per gram respectively, and calculates and returns the total calories generated. You can call the function twice, once for fat grams and once for carb grams. For example,
calculate_calories(30.5 , 9) will return 274.5
calculate_calories(75.8, 4) will return 303.2
#program
#calories from fat= fat_Grams * calories per gram of fat
#calories from carbohydrates=carb_grams* 4
def calculate_calories(taken_grams,cal_per_gram):
#checking whether user given -ve info for taken
grams parameter
if taken_grams<0:
print("enter non
negative value for taken_grams")
#if error occurs it will
return None
return None
#by using the above formula
total_calories=taken_grams*cal_per_gram
#return the totl calories
return total_calories
#calling the function for fats
fat_calories=calculate_calories(30.5,9)
print("fat_calories:= ",fat_calories)
#calling the function for carbohydrates as cal per gram for
carbohydrates is 4
carb_calories=calculate_calories(75.8,4)
print("carb_calories:= ",carb_calories)
#checking input with negative value for taken_grams
calculate_calories(-3,9)
#output
fat_calories:= 274.5
carb_calories:= 303.2
enter non negative value for taken_grams
#code snippet
#calories from fat= fat_Grams * calories per gram of fat
#calories from carbohydrates=carb_grams* 4
def calculate_calories(taken_grams,cal_per_gram):
#checking whether user given -ve info for taken grams parameter
if taken_grams<0:
print("enter non negative value for taken_grams")
#if error occurs it will return None
return None
#by using the above formula
total_calories=taken_grams*cal_per_gram
#return the totl calories
return total_calories
#calling the function for fats
fat_calories=calculate_calories(30.5,9)
print("fat_calories:= ",fat_calories)
#calling the function for carbohydrates as cal per gram for carbohydrates is 4
carb_calories=calculate_calories(75.8,4)
print("carb_calories:= ",carb_calories)
#checking input with negative value for taken_grams
calculate_calories(-3,9)
#screenshots of code
#screenshot of output