From df82f75030e5c4f8b2cca1246e7902bcdebf5789 Mon Sep 17 00:00:00 2001 From: "xuanyi.wang" Date: Mon, 3 Apr 2023 20:15:10 +0200 Subject: [PATCH] Update page 'Day 3 homework - Sloetjes' --- Day 3 homework - Sloetjes.md | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/Day 3 homework - Sloetjes.md b/Day 3 homework - Sloetjes.md index 98fc601..f0a67ee 100644 --- a/Day 3 homework - Sloetjes.md +++ b/Day 3 homework - Sloetjes.md @@ -95,8 +95,8 @@ class BoundedInt: def __repr__(self): return str(self.i) - ``` + **8. Modify the template so it also supports adding a regular integer to a BoundedInt, to pass test 4. Note that you can use isinstance() to determine the class of a variable.** ``` class BoundedInt(): @@ -117,22 +117,8 @@ class BoundedInt(): if result > self.max: raise ValueError("Result exceeds max value") return BoundedInt(result, self.min, self.max) - - def __sub__(self, other): - """ - Subtracts another BoundedInt or an int from this BoundedInt. - - Raises: - ValueError: If subtracting a BoundedInt would exceed self.min. - """ - if isinstance(other, BoundedInt): - result = self.i - other.i - else: - result = self.i - other - if result < self.min: - raise ValueError("Result exceeds min value") - return BoundedInt(result, self.min, self.max) ``` + **9. Same as 8, but for subtraction.** ``` class BoundedInt(): @@ -153,6 +139,7 @@ class BoundedInt(): raise ValueError("Result exceeds min value") return BoundedInt(result, self.min, self.max) ``` + **10. You don't have to implement the code, but think about how you would change the template so it could allow adding a BoundedInt to an emoji, if there was a hypothetical function sentiment(emoji) which returned a floating point number from -10.0 to 10.0 which indicated whether the emoji was happy or sad. e.g. BoundedInt(1, min = 1, max = 10) + 😁 would raise `ValueError`.** ``` @@ -188,7 +175,6 @@ class BoundedInt(): if result < self.min: raise ValueError("Result exceeds min value") return BoundedInt(result, self.min, self.max) - ``` #####An example of `sentiment(emoji)`: ```