ISTA 130 -- Computational Thinking and Doing
(McCann)
Demo Program: Calories Due to Fat
Due Date: Smarch 32nd, 2048, before 8:00 p.m.
Overview: Any dietician will tell you that limiting your daily fat intake to be under 30% of your total calories is a good idea. But how many of the calories in your favorite foods are from fat? Nutrition labels will tell you how many grams of fat are in a serving, and how many total calories are in that serving, but you need to do the rest of the figuring on your own.
As it happens, a gram of fat has about 9 calories. Therefore, if you take the grams of fat in a serving of a particular food, multiply it by 9 and divide by the total calories in the serving, you'll get the fraction of the calories due to fat. To get the result as a percentage, just multiply by 100.
For example, consider a product that has 3 grams of fat and 170 calories per serving. There are 27 calories due to fat (3 * 9), and 27 / 170 = 0.159. Multiplying by 100 gives the final answer: 15.9 percent of the calories are due to fat.
Assignment: Write a complete, well-documented Python program that begins by asking the user to enter the grams of fat and the calories in a serving of a food product. The program will compute the percentage of the food's calories that are due to fat, and will display the input information and the percentage in the form of a complete English sentence.
Data: Here are some examples of typical input that your program is expected to handle:
Fat (grams) Total Calories
----------- --------------
1 60
21 480
Output: Your program's output is to appear on the screen as follows (using the second food item from above as input):
This program will tell you what percentage of the calories
in a serving of a food are from the food's fat content.
How many grams of fat are in one serving? 21
How many total calories are in one serving? 480
A food with 21 grams of fat and 480 calories per serving
has 39.4% of those calories from fat.
Turn In: On the due date, electronically submit your documented Python program.
##########################################################################
# Author: [Your name and email address here! (w/o the brackets, please)]
# Class: ISTA 130, Fall 2010
# SL: [Your SL's name here]
#
# Program: [Assignment # and Name]
# Due Date: [Assignment Due Date and Time]
#
# Language: Python 3
# To Run: [Execution Instructions]
#
# Purpose: [A one-paragraph description of what the program does.]
#
# "Bugs": [A list of remaining problems/omissions, or "None"]
##########################################################################
Note that I've included some brief comments with each label, to let
you know what you need to add. Because deleting all of those comments is
a pain, here's a comment-free version
##########################################################################
# Author:
# Class: ISTA 130, Fall 2010
# SL:
#
# Program:
# Due Date:
#
# Language: Python 3
# To Run:
#
# Purpose:
#
# "Bugs":
##########################################################################
You should be able to fill out all of this template except for the last
section before you write the program. Here's what it might look
like for our example assignment:
##########################################################################
# Author: Keyser Soze (keyser@onestepahead.net)
# Class: ISTA 130, Fall 2010
# SL: A. Elpful Ooondergrod
#
# Program: #-1 (Calories from Fat)
# Due Date: Smarch 32nd, 2048 before 8:00 p.m.
#
# Language: Python 3
# To Run: python3 calories.py
#
# Purpose: This program determines what percentage of the calories
# in a food product are due to the food's fat content, given
# a food item's number of grams of fat and total calories.
#
# "Bugs":
##########################################################################
##########################################################################
# Author: Lester I. McCann
# Class: ISTA 130, Fall 2010
# SL: A. Elpful Ooondergrod
#
# Program: #-1 (Calories from Fat)
# Due Date: Smarch 32nd, 2048, before 8:00 p.m.
#
# Language: Python 3
# To Run: python3 calories.py
#
# Purpose: This program determines what percentage of the calories
# in a food product are due to the food's fat content, given
# a food item's number of grams of fat and total calories.
#
# "Bugs":
##########################################################################
def main():
# Display instructions for the user
# Accept fat grams and calories from the user
# Compute the percentage of calories due to fat
# Echo the input data and display the percentage of calories due to fat
if __name__ == "__main__": main()
def main():
# Display instructions for the user
# Accept fat grams and calories from the user
fat_grams = eval(input("How many grams of fat are in one serving? "))
total_calories = eval(input("How many total calories are in one serving? "))
# Compute the percentage of calories due to fat
# Echo the input data and display the percentage of calories due to fat
print(fat_grams, total_calories)
if __name__ == "__main__": main()
$ python3 calories.py
How many grams of fat are in one serving? 1
How many total calories are in one serving? 2
1 2
The input messages look good already, but of course the output still
needs to be improved. All in due time!
def main():
# Display instructions for the user
# Accept fat grams and calories from the user
fat_grams = eval(input("How many grams of fat are in one serving? "))
total_calories = eval(input("How many total calories are in one serving? "))
# Compute the percentage of calories due to fat
calories_from_fat = fat_grams * 9
percent_calories_from_fat = 100 * calories_from_fat / total_calories
# Echo the input data and display the percentage of calories due to fat
print(fat_grams, total_calories, percent_calories_from_fat)
if __name__ == "__main__": main()
$ python3 cal.py
How many grams of fat are in one serving? 3
How many total calories are in one serving? 170
3 170 15.8823529412
##########################################################################
# Author: Lester I. McCann
# Class: ISTA 130, Fall 2010
# SL: A. Elpful Ooondergrod
#
# Program: #-1 (Calories from Fat)
# Due Date: Smarch 32nd, 2048, before 8:00 p.m.
#
# Language: Python 3
# To Run: python3 calories.py
#
# Purpose: This program determines what percentage of the calories
# in a food product are due to the food's fat content, given
# a food item's number of grams of fat and total calories.
#
# "Bugs": None; this program meets the assignment requirements.
##########################################################################
CALORIES_PER_GRAM = 9 # There are roughly nine calories per gram of fat
def main():
# Display instructions for the user
print()
print("This program will tell you what percentage of the calories\n"+
"in a serving of a food are from the food's fat content.")
print()
# Accept fat grams and calories from the user
fat_grams = eval(input("How many grams of fat are in one serving? "))
total_calories = eval(input("How many total calories are in one serving? "))
# Compute the percentage of calories due to fat
calories_from_fat = fat_grams * CALORIES_PER_GRAM
percent_calories_from_fat = 100 * calories_from_fat / total_calories
# Echo the input data and display the percentage of calories due to fat
if fat_grams == 1:
print("\nA food with %d gram of fat " % fat_grams, end="")
else:
print("\nA food with %d grams of fat " % fat_grams, end="")
if total_calories == 1:
print("and %d calorie per serving" % total_calories)
else:
print("and %d calories per serving" % total_calories)
print("has %.1f%% of those calories from fat." % percent_calories_from_fat)
# main()
if __name__ == "__main__": main()
$ python3 calories.py
This program will tell you what percentage of the calories
in a serving of a food are from the food's fat content.
How many grams of fat are in one serving? 3
How many total calories are in one serving? 170
A food with 3 grams of fat and 170 calories per serving
has 15.9% of those calories from fat.