|
| 1 | +""" |
| 2 | +Food |
| 3 | +
|
| 4 | +Create a Food class with 4 instance |
| 5 | +attributes: name. calories, grams of |
| 6 | +protein, and grams of fat. |
| 7 | +
|
| 8 | +It should have 2 methods: an eat method |
| 9 | +that prints "You are eating " and the name |
| 10 | +of the food, and a burn method that prints |
| 11 | +"You burned [x] calories." where [x] is |
| 12 | +the number of calories in the food. |
| 13 | +
|
| 14 | +Create a JunkFood subclass and a Meal |
| 15 | +subclass. Both subclasses should carry |
| 16 | +over the attributes and methods of the |
| 17 | +Food class. |
| 18 | +The JunkFood subclass should have an |
| 19 | +additional attribute for the grams of |
| 20 | +sugar contained in the food, and the Meal |
| 21 | +subclass should have an additional attribute |
| 22 | +for the mg of sodium it contains. |
| 23 | +
|
| 24 | +Create a list called snacks and fill it with at |
| 25 | +least 3 junk foods, and create a list called |
| 26 | +meals and fill it with at least 3 meals. |
| 27 | +Then, use Python to show that you ate all the |
| 28 | +foods in both lists, and burned off one meal |
| 29 | +(pick this meal randomly). |
| 30 | +Display the total number of calories, |
| 31 | +grams of protein, grams of fat, grams of |
| 32 | +sugar, and mg of sodium that you ate (the total |
| 33 | +for all the foods in both lists). |
| 34 | +""" |
| 35 | +import random |
| 36 | + |
| 37 | + |
| 38 | +class Food: |
| 39 | + def __init__(self, name, cals, protein, fat): |
| 40 | + self.name = name |
| 41 | + self.cals = cals |
| 42 | + self.protein = protein |
| 43 | + self.fat = fat |
| 44 | + |
| 45 | + def eat(self): |
| 46 | + print("You are eating " + self.name + ".") |
| 47 | + |
| 48 | + def burn(self): |
| 49 | + print("You burned %d calories." % self.cals) |
| 50 | + |
| 51 | + |
| 52 | +class JunkFood(Food): |
| 53 | + def __init__(self, name, cals, protein, fat, sugar): |
| 54 | + super().__init__(name, cals, protein, fat) |
| 55 | + self.sugar = sugar |
| 56 | + |
| 57 | + |
| 58 | +class Meal(Food): |
| 59 | + def __init__(self, name, cals, protein, fat, sodium): |
| 60 | + super().__init__(name, cals, protein, fat) |
| 61 | + self.sodium = sodium |
| 62 | + |
| 63 | + |
| 64 | +snacks = [ |
| 65 | + JunkFood("Oreo", 55, 0.5, 2.2, 4.1), |
| 66 | + JunkFood("Brownie", 70, 2, 3, 2), |
| 67 | + JunkFood("Chips", 160, 2, 10, 0), |
| 68 | +] |
| 69 | + |
| 70 | +meals = [ |
| 71 | + Meal("Rice and beans", 400, 10, 5, 15), |
| 72 | + Meal("Burrito", 350, 8, 9, 100), |
| 73 | + Meal("Pizza", 500, 20, 15, 150), |
| 74 | +] |
| 75 | + |
| 76 | +cals, protein, fat, sugar, sodium = 0, 0, 0, 0, 0 |
| 77 | + |
| 78 | +for snack in snacks: |
| 79 | + snack.eat() |
| 80 | + cals += snack.cals |
| 81 | + protein += snack.protein |
| 82 | + fat += snack.fat |
| 83 | + sugar += snack.sugar |
| 84 | + |
| 85 | +for meal in meals: |
| 86 | + meal.eat() |
| 87 | + cals += meal.cals |
| 88 | + protein += meal.protein |
| 89 | + fat += meal.fat |
| 90 | + sodium += meal.sodium |
| 91 | + |
| 92 | +# Choose a random meal to burn off. |
| 93 | +meals[random.randrange(len(meals))].burn() |
| 94 | + |
| 95 | +# Display totals |
| 96 | +print("Totals eaten:") |
| 97 | +print("Calories:", cals) |
| 98 | +print("Protein (g):", protein) |
| 99 | +print("Fat (g):", fat) |
| 100 | +print("Sugar (g):", sugar) |
| 101 | +print("Sodium (mg):", sodium) |
0 commit comments