Created
May 27, 2020 07:46
-
-
Save kurzweil777/408dbfe3b4c1e322ef8dd108b472446d to your computer and use it in GitHub Desktop.
First Attempts in OOP
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
| class Animal: | |
| name = "" | |
| category = "" | |
| def __init__(self, name): | |
| self.name = name | |
| def set_category(self, category): | |
| self.category = category | |
| class Zoo: | |
| def __init__(self): | |
| self.current_animals = {} | |
| def add_animal(self, animal): | |
| self.current_animals[animal.name] = animal.category | |
| def total_of_category(self, category): | |
| result = 0 | |
| for animal in self.current_animals.values(): | |
| if animal == category: | |
| result += 1 | |
| return result | |
| Turtle = Animal('Turtle') | |
| Snake = Animal('Snake') | |
| Turtle.set_category('Reptile') | |
| Snake.set_category('Reptile') | |
| zoo = Zoo() | |
| print(Snake.category) | |
| zoo.add_animal(Turtle) | |
| zoo.add_animal(Snake) | |
| print(zoo.total_of_category('Reptile')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great activity on code reuse! My Zoo!