From 59544de0409dead97a5b41dbd43bb315dfe470be Mon Sep 17 00:00:00 2001 From: Neha Peddinti Date: Sun, 2 Aug 2020 23:40:54 -0400 Subject: [PATCH 1/6] Add Ch. 12 problem --- .../chapter12/practice/food_class.py | 34 ++++++ .../chapter12/solutions/food_class.py | 101 ++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 2_intermediate/chapter12/practice/food_class.py create mode 100644 2_intermediate/chapter12/solutions/food_class.py diff --git a/2_intermediate/chapter12/practice/food_class.py b/2_intermediate/chapter12/practice/food_class.py new file mode 100644 index 00000000..868eeef3 --- /dev/null +++ b/2_intermediate/chapter12/practice/food_class.py @@ -0,0 +1,34 @@ +""" +Food + +Create a Food class with 4 instance +attributes: name. calories, grams of +protein, and grams of fat. + +It should have 2 methods: an eat method +that prints "You are eating " and the name +of the food, and a burn method that prints +"You burned [x] calories." where [x] is +the number of calories in the food. + +Create a JunkFood subclass and a Meal +subclass. Both subclasses should carry +over the attributes and methods of the +Food class. +The JunkFood subclass should have an +additional attribute for the grams of +sugar contained in the food, and the Meal +subclass should have an additional attribute +for the mg of sodium it contains. + +Create a list called snacks and fill it with at +least 3 junk foods, and create a list called +meals and fill it with at least 3 meals. +Then, use Python to show that you ate all the +foods in both lists, and burned off one meal +(pick this meal randomly). +Display the total number of calories, +grams of protein, grams of fat, grams of +sugar, and mg of sodium that you ate (the total +for all the foods in both lists). +""" diff --git a/2_intermediate/chapter12/solutions/food_class.py b/2_intermediate/chapter12/solutions/food_class.py new file mode 100644 index 00000000..ee7849d2 --- /dev/null +++ b/2_intermediate/chapter12/solutions/food_class.py @@ -0,0 +1,101 @@ +""" +Food + +Create a Food class with 4 instance +attributes: name. calories, grams of +protein, and grams of fat. + +It should have 2 methods: an eat method +that prints "You are eating " and the name +of the food, and a burn method that prints +"You burned [x] calories." where [x] is +the number of calories in the food. + +Create a JunkFood subclass and a Meal +subclass. Both subclasses should carry +over the attributes and methods of the +Food class. +The JunkFood subclass should have an +additional attribute for the grams of +sugar contained in the food, and the Meal +subclass should have an additional attribute +for the mg of sodium it contains. + +Create a list called snacks and fill it with at +least 3 junk foods, and create a list called +meals and fill it with at least 3 meals. +Then, use Python to show that you ate all the +foods in both lists, and burned off one meal +(pick this meal randomly). +Display the total number of calories, +grams of protein, grams of fat, grams of +sugar, and mg of sodium that you ate (the total +for all the foods in both lists). +""" +import random + + +class Food: + def __init__(self, name, cals, protein, fat): + self.name = name + self.cals = cals + self.protein = protein + self.fat = fat + + def eat(self): + print("You are eating " + self.name + ".") + + def burn(self): + print("You burned %d calories." % self.cals) + + +class JunkFood(Food): + def __init__(self, name, cals, protein, fat, sugar): + Food.__init__(self, name, cals, protein, fat) + self.sugar = sugar + + +class Meal(Food): + def __init__(self, name, cals, protein, fat, sodium): + Food.__init__(self, name, cals, protein, fat) + self.sodium = sodium + + +snacks = [ + JunkFood("Oreo", 55, 0.5, 2.2, 4.1), + JunkFood("Brownie", 70, 2, 3, 2), + JunkFood("Chips", 160, 2, 10, 0) +] + +meals = [ + Meal("Rice and beans", 400, 10, 5, 15), + Meal("Burrito", 350, 8, 9, 100), + Meal("Pizza", 500, 20, 15, 150) +] + +cals = protein = fat = sugar = sodium = 0 + +for snack in snacks: + snack.eat() + cals += snack.cals + protein += snack.protein + fat += snack.fat + sugar += snack.sugar + +for meal in meals: + meal.eat() + cals += meal.cals + protein += meal.protein + fat += meal.fat + sodium += meal.sodium + +# Choose a random meal to burn off. +meals[random.randrange(len(meals))].burn() + +# Display totals +print("Totals eaten:") +print("Calories:", cals) +print("Protein (g):", protein) +print("Fat (g):", fat) +print("Sugar (g):", sugar) +print("Sodium (mg):", sodium) From e7486568b261b313ff55b44f0b14bf79bfc75cce Mon Sep 17 00:00:00 2001 From: Lint Action Date: Mon, 3 Aug 2020 03:43:02 +0000 Subject: [PATCH 2/6] Fix code style issues with Black --- 2_intermediate/chapter12/solutions/food_class.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2_intermediate/chapter12/solutions/food_class.py b/2_intermediate/chapter12/solutions/food_class.py index ee7849d2..e1ef68f8 100644 --- a/2_intermediate/chapter12/solutions/food_class.py +++ b/2_intermediate/chapter12/solutions/food_class.py @@ -64,13 +64,13 @@ def __init__(self, name, cals, protein, fat, sodium): snacks = [ JunkFood("Oreo", 55, 0.5, 2.2, 4.1), JunkFood("Brownie", 70, 2, 3, 2), - JunkFood("Chips", 160, 2, 10, 0) + JunkFood("Chips", 160, 2, 10, 0), ] meals = [ Meal("Rice and beans", 400, 10, 5, 15), Meal("Burrito", 350, 8, 9, 100), - Meal("Pizza", 500, 20, 15, 150) + Meal("Pizza", 500, 20, 15, 150), ] cals = protein = fat = sugar = sodium = 0 From 516f4bae5202141f19ddd4ff862f8004cedcfdad Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Tue, 4 Aug 2020 17:48:32 -0400 Subject: [PATCH 3/6] Commit changes in __init__ method Co-authored-by: abhatia1205 <32373316+abhatia1205@users.noreply.github.com> --- 2_intermediate/chapter12/solutions/food_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_intermediate/chapter12/solutions/food_class.py b/2_intermediate/chapter12/solutions/food_class.py index e1ef68f8..53a2f667 100644 --- a/2_intermediate/chapter12/solutions/food_class.py +++ b/2_intermediate/chapter12/solutions/food_class.py @@ -51,7 +51,7 @@ def burn(self): class JunkFood(Food): def __init__(self, name, cals, protein, fat, sugar): - Food.__init__(self, name, cals, protein, fat) + super().__init__(name, cals, protein, fat) self.sugar = sugar From 4ef5ae8e230b3d6a830be9cf5fa1a9d3379cc980 Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Tue, 4 Aug 2020 17:48:55 -0400 Subject: [PATCH 4/6] Update food_class.py Co-authored-by: abhatia1205 <32373316+abhatia1205@users.noreply.github.com> --- 2_intermediate/chapter12/solutions/food_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_intermediate/chapter12/solutions/food_class.py b/2_intermediate/chapter12/solutions/food_class.py index 53a2f667..53a74c20 100644 --- a/2_intermediate/chapter12/solutions/food_class.py +++ b/2_intermediate/chapter12/solutions/food_class.py @@ -73,7 +73,7 @@ def __init__(self, name, cals, protein, fat, sodium): Meal("Pizza", 500, 20, 15, 150), ] -cals = protein = fat = sugar = sodium = 0 +cals, protein, fat, sugar, sodium = 0,0,0,0,0 for snack in snacks: snack.eat() From dbd6cd649ea08e959fd1cbeed976ba2e42fb1fc4 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Tue, 4 Aug 2020 21:49:16 +0000 Subject: [PATCH 5/6] Fix code style issues with Black --- 2_intermediate/chapter12/solutions/food_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_intermediate/chapter12/solutions/food_class.py b/2_intermediate/chapter12/solutions/food_class.py index 53a74c20..fe901dbf 100644 --- a/2_intermediate/chapter12/solutions/food_class.py +++ b/2_intermediate/chapter12/solutions/food_class.py @@ -73,7 +73,7 @@ def __init__(self, name, cals, protein, fat, sodium): Meal("Pizza", 500, 20, 15, 150), ] -cals, protein, fat, sugar, sodium = 0,0,0,0,0 +cals, protein, fat, sugar, sodium = 0, 0, 0, 0, 0 for snack in snacks: snack.eat() From 39bd2f97fc6202c64e4cc61233493b015c21af8c Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Tue, 4 Aug 2020 17:51:43 -0400 Subject: [PATCH 6/6] Update food_class.py Co-authored-by: abhatia1205 <32373316+abhatia1205@users.noreply.github.com> --- 2_intermediate/chapter12/solutions/food_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_intermediate/chapter12/solutions/food_class.py b/2_intermediate/chapter12/solutions/food_class.py index fe901dbf..5500c123 100644 --- a/2_intermediate/chapter12/solutions/food_class.py +++ b/2_intermediate/chapter12/solutions/food_class.py @@ -57,7 +57,7 @@ def __init__(self, name, cals, protein, fat, sugar): class Meal(Food): def __init__(self, name, cals, protein, fat, sodium): - Food.__init__(self, name, cals, protein, fat) + super().__init__(name, cals, protein, fat) self.sodium = sodium