From 72917a61d66deb68023bdaaa807e551160e998eb Mon Sep 17 00:00:00 2001 From: Karenzz Date: Mon, 3 Apr 2023 16:11:15 +0200 Subject: [PATCH] Update page 'Day 3 Homework - Karen' --- Day 3 Homework - Karen.md | 74 +++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/Day 3 Homework - Karen.md b/Day 3 Homework - Karen.md index a9168f2..d298b1f 100644 --- a/Day 3 Homework - Karen.md +++ b/Day 3 Homework - Karen.md @@ -22,16 +22,70 @@ class BoundedInt(): self.max = max ``` ​ -1. Read this: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__int__ -2. Modify the template to pass test 1, which checks that `int()` called on your class will return the value of `i`. -3. Read this, and the paragraph below it: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__add__ -4. What do you think the types of x and y are? Guess, without checking first. What are they, actually? You can use the debugger's local window to find out. -5. Don't forget that you can access the value of a BoundedInt by using either `.i` on it, or `int()`, now that you've passed test 1. -6. Modify the template to pass test 2, which checks that: +**1. Read this: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__int__** + +**2. Modify the template to pass test 1, which checks that `int()` called on your class will return the value of `i`.** +``` +class BoundedInt(): + def __init__(self, i, min, max): + """ + Create a bounded integer. + + Args: + i (int): The initial value of the integer. + min (int): The minimum value of the integer. + max (int): The maximum value of the integer. + """ + ''' + self.i = max if i > max else min if i < min else i + self.min = min + self.max = max +``` +It's the first draft I have made to meet the requirement; however, the result returns to be "❌ Test 2 failed!", so I want to check i to see what happends to this result and the modified `BoundedInt` is shown as: + +``` +class BoundedInt(): + def __init__(self, i, min, max): + """ + Create a bounded integer. + + Args: + i (int): The initial value of the integer. + min (int): The minimum value of the integer. + max (int): The maximum value of the integer. + """ + ''' + self.i = max if i > max else min if i < min else i + self.min = min + self.max = max + ''' + if i < min or i > max: + raise ValueError(f"Initial value {i} is outside the range [{min}, {max}]") + self.i = i + self.min = min + self.max = max + + + def __int__(self): + return self.i +``` +The result after runnning this modified script says "Initial value 3735928559 is outside the range [10, 0]". If we change the range defined in `BoundedIntTester` to be [0, 1000000000000], then it will return "✅ Test 1 passed!". + +**3. Read this, and the paragraph below it: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__add__** + +**4. What do you think the types of x and y are? Guess, without checking first. What are they, actually? You can use the debugger's local window to find out.** + +**5. Don't forget that you can access the value of a BoundedInt by using either `.i` on it, or `int()`, now that you've passed test 1.** + +**6. Modify the template to pass test 2, which checks that:** * Your class implements the + operator. * It throws ValueError when attempting to add another BoundedInt that would exceed `self.max`. -7. Do the same as 6, but for subtraction, to pass test 3. -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. -9. Same as 8, but for subtraction. -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`. + +**7. Do the same as 6, but for subtraction, to pass test 3.** + +**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.** + +**9. Same as 8, but for subtraction.** + +**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`.** ​ \ No newline at end of file