상속한 클래스에 메서드 추가하기
이제 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(____, ____)