Classes#
1. Fill the missing pieces of the Calculator
class#
Fill ____
pieces of the Calculator
implemention in order to pass the assertions.
class Calculator:
def __init__(self, var1, ____):
self.____ = var1
self.____ = _____
def calculate_power(self):
return self.____ ** ____.____
def calculate_sum(____, var3):
return ____.____ + ____.____ + var3
calc = Calculator(2, 3)
assert calc.calculate_power() == 8
assert calc.calculate_sum(4) == 9
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 1
----> 1 calc = Calculator(2, 3)
2 assert calc.calculate_power() == 8
3 assert calc.calculate_sum(4) == 9
Cell In[1], line 4, in Calculator.__init__(self, var1, ____)
2 def __init__(self, var1, ____):
3 self.____ = var1
----> 4 self.____ = _____
NameError: name '_____' is not defined
2. Finalize StringManipulator
class#
Fill ____
pieces and create implementation for stripped_title()
.
class StringManipulator:
"""____"""
category = ____
def __init__(self, original):
self.string = ____
def reverse_words(self):
words = self.string.____
self.string = ' '.join(reversed(____))
def make_title(self):
# Create implementation for this
def get_manipulated(____):
return self._____
assert StringManipulator.__doc__ == 'Docstring of StringManipulator'
assert StringManipulator.category == 'Manipulator'
str_manip = StringManipulator('cOOL pyThON')
str_manip.reverse_words()
assert str_manip.get_manipulated() == 'pyThON cOOL'
str_manip.make_title()
assert str_manip.get_manipulated() == 'Python Cool'
3. Create Dog
class#
Create Dog
class which has the following specification:
Dogs consume their energy by barking and gain energy by sleeping
A fresh
Dog
instance has 10 units of energyDog
has a methodsleep
which gives 2 units of energyDog
has a methodbark
which consumes 1 unit of energyDog
has a methodget_energy
which returns the amount of energy left
class Dog:
# Your implementation here
doge = Dog()
assert doge.get_energy() == 10
doge.bark()
doge.bark()
doge.bark()
assert doge.get_energy() == 7
doge.sleep()
assert doge.get_energy() == 9
another_doge = Dog()
assert another_doge.get_energy() == 10