Question

Design a program that asks for the number of fat grams and calories in a food...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Fat Grams and Secondary Schools. The number of fat calories and grams of saturated fat in...
Fat Grams and Secondary Schools. The number of fat calories and grams of saturated fat in a number of fast-food non-breakfast entrees are shown below. Fat Calories 190 220 270 360 460 540 Sat. Fat 9 8 13 17 23 27 Compute the value of the correlation coefficient?
Fat Grams and Secondary Schools. The number of fat calories and grams of saturated fat in...
Fat Grams and Secondary Schools. The number of fat calories and grams of saturated fat in a number of fast-food non-breakfast entrees are shown below. Fat Calories 190 220 270 360 460 540 Sat. Fat 9 8 13 17 23 27 Compute the t statistics for testing the correlation between variables; Null hypothesis H0: ρ = 0 Alternative hypothesis HA: ρ ≠ 0 or HA: ρ < 0 or HA: ρ > 0 Second, we calculate the value of the...
Fat Grams and Secondary Schools. The number of fat calories and grams of saturated fat in...
Fat Grams and Secondary Schools. The number of fat calories and grams of saturated fat in a number of fast-food non-breakfast entrees are shown below. Fat Calories 190 220 270 360 460 540 Sat. Fat 9 8 13 17 23 27 Questions: a) In a scatter plot, what variable goes on vertical axis? b) In a scatter plot, what variable goes on the horizontal axis? c) Are the two variables positively or negatively correlated? Explain
DESIGN A FLOWCHART IN FLOWGORITHM Number Analysis Program Design a program that asks the user to...
DESIGN A FLOWCHART IN FLOWGORITHM Number Analysis Program Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display the following data: The lowest number in the array. The highest number in the array. The total of the numbers in the array. The average of the numbers in the array. PLEASE AND THANK YOU
DESIGN A FLOWCHART IN FLOWGORITHM STEP BY STEP Number Analysis Program Design a program that asks...
DESIGN A FLOWCHART IN FLOWGORITHM STEP BY STEP Number Analysis Program Design a program that asks the user to enter a maximum of 20 numbers. The program should store the numbers in an array and then display the following data: -The lowest number in the array. -The highest number in the array. -The total of the numbers in the array. -The average of the numbers in the array. PLEASE AND THANK YOU
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
1. Write a complete program in C++ that does the following: [2] asks a user to...
1. Write a complete program in C++ that does the following: [2] asks a user to enter two integer numbers (display a warning message that the second number should be different from zero) [3] reads the two numbers from the keyboard; [4] displays the product, the sum, the quotient and the remainder of integer division of the first one by the second one. [3] displays the result of floating-point division. Make sure to follow programming style guidelines.
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT