-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add Ch. 12 problem #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
59544de
Add Ch. 12 problem
e748656
Fix code style issues with Black
lint-action 80aaf41
Merge branch 'master' into neha-add-exercises
phrdang 516f4ba
Commit changes in __init__ method
neha-peddinti 4ef5ae8
Update food_class.py
neha-peddinti dbd6cd6
Fix code style issues with Black
lint-action 39bd2f9
Update food_class.py
neha-peddinti 5fffbb8
Merge branch 'master' into neha-add-exercises
Citrus716 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). | ||
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.