Libraries and Modules in Python

Solutions

In [12]:
class Person:
    """Class that holds a person's height"""
    def __init__(self, height=0, weight=0):
        """Construct a person with the specified name, height and weight"""
        self.setHeight(height)
        self.setWeight(weight)
    def setHeight(self, height):
        """Set the person's height in meters"""
        if height < 0 or height > 2.5:
            raise ValueError("Invalid height: %s. This shoud be between 0 and 2.5 meters" % height)
        self._height = height
    def setWeight(self, weight):
        """Set the person's weight in kilograms"""
        if weight < 0 or weight > 500:
            raise ValueError("Invalid weight: %s. This should be between 0 and 500 kilograms" % weight)
        self._weight = weight
    def getHeight(self):
        """Return the person's height in meters"""
        return self._height
    def getWeight(self):
        """Return the person's weight in kilograms"""
        return self._weight
    def bmi(self):
        """Return the person's body mass index (bmi)"""
        return self.getWeight() / self.getHeight()**2
In [13]:
p = Person(height=2.8, weight=500)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-dbda27e19344> in <module>
----> 1 p = Person(height=2.8, weight=500)
<ipython-input-12-159d49586c15> in __init__(self, height, weight)
      3     def __init__(self, height=0, weight=0):
      4         """Construct a person with the specified name, height and weight"""
----> 5         self.setHeight(height)
      6         self.setWeight(weight)
      7 
<ipython-input-12-159d49586c15> in setHeight(self, height)
      9         """Set the person's height in meters"""
     10         if height < 0 or height > 2.5:
---> 11             raise ValueError("Invalid height: %s. This shoud be between 0 and 2.5 meters" % height)
     12         self._height = height
     13 
ValueError: Invalid height: 2.8. This shoud be between 0 and 2.5 meters
In [14]:
p = Person(height=1.8, weight=501)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-184bd778a834> in <module>
----> 1 p = Person(height=1.8, weight=501)
<ipython-input-12-159d49586c15> in __init__(self, height, weight)
      4         """Construct a person with the specified name, height and weight"""
      5         self.setHeight(height)
----> 6         self.setWeight(weight)
      7 
      8     def setHeight(self, height):
<ipython-input-12-159d49586c15> in setWeight(self, weight)
     15         """Set the person's weight in kilograms"""
     16         if weight < 0 or weight > 500:
---> 17             raise ValueError("Invalid weight: %s. This should be between 0 and 500 kilograms" % weight)
     18         self._weight = weight
     19 
ValueError: Invalid weight: 501. This should be between 0 and 500 kilograms
In [15]:
p = Person(height=1.8, weight=150)

2

In [16]:
class NullPersonError(Exception):
    pass
In [17]:
class Person:
    """Class that holds a person's height"""
    def __init__(self, height=0, weight=0):
        """Construct a person with the specified name, height and weight"""
        self.setHeight(height)
        self.setWeight(weight)
    def setHeight(self, height):
        """Set the person's height in meters"""
        if height < 0 or height > 2.5:
            raise ValueError("Invalid height: %s. This shoud be between 0 and 2.5 meters" % height)
        self._height = height
    def setWeight(self, weight):
        """Set the person's weight in kilograms"""
        if weight < 0 or weight > 500:
            raise ValueError("Invalid weight: %s. This should be between 0 and 500 kilograms" % weight)
        self._weight = weight
    def getHeight(self):
        """Return the person's height in meters"""
        return self._height
    def getWeight(self):
        """Return the person's weight in kilograms"""
        return self._weight
    def bmi(self):
        """Return the person's body mass index (bmi)"""
        if (self.getHeight() == 0 or self.getWeight() == 0):
            raise NullPersonError("Cannot calculate the BMI of a person with zero "
                                  "height or weight (%s,%s)" % (self.getHeight(),self.getWeight()))
        return self.getWeight() / self.getHeight()**2
In [18]:
p = Person()
In [19]:
p.bmi()
---------------------------------------------------------------------------
NullPersonError                           Traceback (most recent call last)
<ipython-input-19-cee8a32df83c> in <module>
----> 1 p.bmi()
<ipython-input-17-cda7d9db6df1> in bmi(self)
     30         if (self.getHeight() == 0 or self.getWeight() == 0):
     31             raise NullPersonError("Cannot calculate the BMI of a person with zero "
---> 32                                   "height or weight (%s,%s)" % (self.getHeight(),self.getWeight()))
     33 
     34         return self.getWeight() / self.getHeight()**2
NullPersonError: Cannot calculate the BMI of a person with zero height or weight (0,0)
In [20]:
p = Person(height=1.8, weight=77.5)
In [21]:
p.bmi()
Out[21]:
23.919753086419753