ComeçarComece de graça

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.

Este exercício faz parte do curso

Case Study: Building Software in Python

Ver curso

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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(____, ____)
Editar e executar o código