|
9 | 9 | from ..utils import facts |
10 | 10 |
|
11 | 11 | import difflib |
| 12 | +import math |
12 | 13 |
|
13 | 14 | class Cat: |
14 | 15 |
|
@@ -210,4 +211,53 @@ def play(self, mood_boost=1, hunger_boost=1, energy_boost=-1): |
210 | 211 | self.mood += mood_boost |
211 | 212 | self.hunger_level += hunger_boost |
212 | 213 | self.energy += energy_boost |
| 214 | + |
| 215 | + def sleep(self, duration=0): |
| 216 | + """ |
| 217 | + Simulates the cat getting some sleep. |
| 218 | +
|
| 219 | + Sleep() causes the cat to sleep for an optionally-specified duration (hrs; default=0). |
| 220 | + For every 3 hours the cat sleeps, its energy level increases increases by 1 (rounded down |
| 221 | + to the nearest integer). For example, having the cat sleeping for a duration of 5 hours raises |
| 222 | + its energy level by 1. |
| 223 | +
|
| 224 | + Parameters |
| 225 | + ---------- |
| 226 | + duration : int or float, optional |
| 227 | + Number of hours the cat sleeps. Must be an integer or float. The default is 0. |
| 228 | +
|
| 229 | + Raises |
| 230 | + ------ |
| 231 | + TypeError |
| 232 | + If duration is neither an integer nor float. |
| 233 | + ValueError |
| 234 | + If duration is not positive or is greater than 16. |
| 235 | + |
| 236 | + Examples |
| 237 | + -------- |
| 238 | + |
| 239 | + ..jupyter-execute:: |
| 240 | + |
| 241 | + import pyCatSim as cats |
| 242 | + nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell') |
| 243 | + nutmeg.sleep(duration=5) |
| 244 | +
|
| 245 | + """ |
| 246 | + |
| 247 | + # Enforce duration type is int or float |
| 248 | + if type(duration) != int: |
| 249 | + if type(duration) != float: |
| 250 | + raise TypeError("duration must be an integer or float") |
| 251 | + |
| 252 | + # Enforce min (0 hrs) and max duration (16 hrs) |
| 253 | + if duration < 0: |
| 254 | + raise ValueError("Cats cannot sleep for negative hours. User-specified duration must be positive") |
| 255 | + if duration > 16: |
| 256 | + raise ValueError("Cats should not sleep for more than 16 hours. User-specified duration must be less than 16") |
| 257 | + |
| 258 | + # Cat gains 1 energy level for every 3 hours of sleep (rounded-down; floor()) |
| 259 | + energy_boost = math.floor(duration/3) |
| 260 | + |
| 261 | + self.energy += energy_boost |
| 262 | + |
213 | 263 |
|
0 commit comments