• JUPYTER
  • FAQ
  • View as Code
  • Python 3 (ipykernel) Kernel
  • View on GitHub
  • Execute on Binder
  • Download Notebook
  1. learn-python3
  2. notebooks
  3. beginner
  4. exercises

1. Fill the missing pieces of the Calculator class¶

Fill ____ pieces of the Calculator implemention in order to pass the assertions.

In [ ]:
class Calculator:
    def __init__(self, var1, ____):
        self.____ = var1
        self.____ = _____

    def calculate_power(self):
        return self.____**____.____

    def calculate_sum(____, var3):
        return ____.____ + ____.____ + var3
In [ ]:
calc = Calculator(2, 3)
assert calc.calculate_power() == 8
assert calc.calculate_sum(4) == 9

2. Finalize StringManipulator class¶

Fill ____ pieces and create implementation for stripped_title().

In [ ]:
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._____
In [ ]:
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 energy
  • Dog has a method sleep which gives 2 units of energy
  • Dog has a method bark which consumes 1 unit of energy
  • Dog has a method get_energy which returns the amount of energy left
In [ ]:
class Dog:
    # Your implementation here
In [ ]:
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

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by OVHcloud

nbviewer GitHub repository.

nbviewer version: 8b013f7

nbconvert version: 7.2.3

Rendered (Tue, 10 Jun 2025 07:09:25 UTC)