-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfood_class.py
More file actions
101 lines (81 loc) · 2.48 KB
/
food_class.py
File metadata and controls
101 lines (81 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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):
super().__init__(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, 0, 0, 0, 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)