|
8 | 8 |
|
9 | 9 | class Owner: |
10 | 10 | """ |
11 | | - Represents a cat owner who can care for one or more cats. |
| 11 | + Represents a cat owner who can care for one or more cats |
12 | 12 |
|
13 | 13 | Parameters |
14 | 14 | ---------- |
@@ -60,6 +60,55 @@ def __init__(self, name, cats_owned): |
60 | 60 |
|
61 | 61 | self.name = name |
62 | 62 | self.cats_owned = cats_owned |
| 63 | + |
| 64 | + def adopt(self, cats_object): |
| 65 | + |
| 66 | + """ |
| 67 | + Add a Cat object or a list of Cat objects to an owner's cats. |
| 68 | +
|
| 69 | + Parameters |
| 70 | + ---------- |
| 71 | + cats_object: the cat to be added to the list |
| 72 | +
|
| 73 | + Raises |
| 74 | + ------ |
| 75 | + TypeError |
| 76 | + If any of the arguments are not Cat. |
| 77 | + ValueError |
| 78 | + If cats_object is not Cat. |
| 79 | +
|
| 80 | + Examples |
| 81 | + -------- |
| 82 | +
|
| 83 | + .. jupyter-execute:: |
| 84 | +
|
| 85 | + import pyCatSim as cats |
| 86 | +
|
| 87 | + cat1 = cats.Cat(name="Whiskers") |
| 88 | + cat2 = cats.Cat(name="Boots", color="tabby") |
| 89 | + owner1 = Owner(name="Sasha", cats_owned=cat1) |
| 90 | + owner2 = Owner(name="Liam", cats_owned=[cat1, cat2]) |
| 91 | +
|
| 92 | + chestnut = cats.Cat(name='Chestnut', age = 4, color = 'tabby') |
| 93 | + nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell') |
| 94 | + owner1.adopt(owner1,nutmeg) |
| 95 | + owner2.adopt(owner2,[chestnug,nutmeg]) |
| 96 | +
|
| 97 | + print(owner1.name) |
| 98 | + print([cat.name for cat in owner2.cats_owned]) |
| 99 | +
|
| 100 | +
|
| 101 | + """ |
| 102 | + if isinstance(cats_object, Cat): |
| 103 | + self.cats_owned.append(cats_object) |
| 104 | + elif isinstance(cats_object, list): |
| 105 | + if not all(isinstance(cat, Cat) for cat in cats_object): |
| 106 | + raise TypeError("All elements in cats_object must be instances of Cat.") |
| 107 | + else: |
| 108 | + self.cats_owned+=cats_object |
| 109 | + else: |
| 110 | + raise TypeError("cats_owned must be a Cat instance or a list of Cat instances.") |
| 111 | + |
63 | 112 |
|
64 | 113 | def groom(self,Cat): |
65 | 114 | """ |
|
0 commit comments