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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started