Design a program that asks for the number of fat grams and calories in a food item. Validate the input as follows: Make sure the number of fat grams and calories is not less than 0. According to nutritional formulas, the number of calories cannot exceedfatgrams×9. Make sure that the number of calories entered is not greater than fatgrams×9. Once correct data has been entered, the program should calculate and display the percentage of calories that come from fat. Use the following formula: Percentageofcaloriesfromfat=(Fatgrams×9)÷Calories Some nutritionists classify a food as “low fat” if less than 30 percent of its calories come from fat. If the results of this formula are less than 0.3, the program should display a message indicating the food is low in fat.
Please find the code and execution of the sample output screenshot below.
I have commented code at most of the cases inorder to make it clear for you. Please find below.
#reading both the values fat_grams and calories from the user
fat_grams = int(input("Enter the number of fat grams :"))
calories = int(input("Enter the number of calories :"))
#checking the values of fat_grams and calories are matching our criteria or not, if now we will read the input again
while ((fat_grams < 0 ) or (calories <= 0) or (calories > 9*fat_grams)):
if((fat_grams < 0 )):
fat_grams = int(input("please Enter the number of fat grams value >= 0 :\n"))
if((calories <= 0)):
calories = int(input("Please Enter the number of calories value > 0 :\n"))
if (calories > 9*fat_grams):
calories = int(input("Please Enter the number of calories value <= : 9*fat_grams\n"))
print("Calculating the percentage of calories that come from fat \n")
#calculating the percentage_of_calories as required by you
percentage_of_calories = (fat_grams*9)/calories
#displayin the percentage_of_calories
print("Percentage of calories that come from fat is :", percentage_of_calories)
#if calories are less than 0.3 the we are displaying the LOW fat indication
if(percentage_of_calories < 0.3):
print("Food is LOW in fat")
Screenshot of the execution:
2nd sample:
Get Answers For Free
Most questions answered within 1 hours.