시작하기무료로 시작하기

코드 리팩터링

코드 리뷰 후 소프트웨어 공학의 모범 사례를 적용해 코드를 리팩터링해 보려고 해요. 예를 들어, 총 상환 횟수는 0보다 작아서는 안 돼요. 이런 상황이 발생하면 에러를 던지세요.

이 연습은 강의의 일부입니다

케이스 스터디: Python으로 소프트웨어 만들기

강의 보기

연습 안내

  • 대출의 총 개월 수를 나타내는 변수로 에러 조건을 완성하세요.
  • 월 상환 횟수가 0보다 작으면 ValueError를 던지세요.
  • 대출 금액을 반환하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

def calculate_loan_amount(monthly_payment, monthly_interest_rate, nbr_payments):
    """
    Calculate the loan amount based on the monthly payment, monthly interest rate, and total number of payments.
    """	
    # Raise an error if the number of total payment is less than zero
    if ____ <= 0:
        raise ____("The number of payments must be greater than zero.")

    loan_amount = (monthly_payment * ((1 + monthly_interest_rate) ** nbr_payments - 1)) / \
                  (monthly_interest_rate * (1 + monthly_interest_rate) ** nbr_payments)
    
    # Return the loan amount
    return ____
코드 편집 및 실행