始める無料で始める

継承したクラスにメソッドを追加する

MortgageCalculator に、元金、年利、返済年数から毎月の返済額を計算するメソッドを追加しましょう。

この演習はコースの一部です

ケーススタディ:Pythonでソフトウェアを構築する

コースを見る

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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(____, ____)
コードを編集して実行