Get startedGet started for free

Add a method to an inherited class

Let's add a method to the MortgageCalculator to calculate the monthly payment given a principal amount, an annual interest rate, and the number of years to pay off the loan.

This exercise is part of the course

Case Study: Building Software in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class MortgageCalculator(FinancialCalculator):
  def __init__(self, loan_amount, annual_interest_rate, years):
    super().__init__()
    self.loan_amount = loan_amount
    self.monthly_interest_rate = self.monthly_interest(annual_interest_rate)
    self.months = self.multiply(years, 12)
    self.monthly_payment = self.calculate_monthly_payment()
  
  def calculate_monthly_payment(self):
    numerator = self.monthly_interest_rate * (1 + self.monthly_interest_rate) ** self.months
    denominator = (1 + self.monthly_interest_rate) ** self.months - 1
    
    # Compute the quotient of numerator and denominator and save the result to multiplier
    multiplier = self.divide(____, ____)
Edit and Run Code