Question: In Python code: Write a program to calculate the percentage of fat calories in a food item. Your program should allow the total number of

In Python code:

Write a program to calculate the percentage of fat calories in a food item. Your program should allow the total number of calories in the food item and the number of grams of fat. Validate the input as follows:

  • Make sure the number of calories and the number of fat grams are not less than 0.
  • There are 9 calories in every gram of fat. The calories from fat must not exceed the total calories in the food item. Make sure that the number of calories entered is not greater than fat grams times 9.

Tell the user if the item is a low fat food item. A low fat food gets 30% or less of its calories from fat.

Need to use defensive programming. (All numbers entered are examples)

Example output should look as follows:

How many calories per serving? -240 (user enters this)

Number of calories must not be negative.

How many calories per serving? 240 (user enters this)

How many grams of fat? -5 (user enters this)

Number of grams of fat must not be negative and calories from fat cannot exceed the total calories.

How many grams of fat? 30 (user enters this)

Number of grams of fat must not be negative and calories from fat cannot exceed the total calories.

How many grams of fat? 5 (user enters this)

18.75% of calories from fat. This is a low fat food.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!