BaşlayınÜcretsiz Başlayın

Refactor Code

You decide to refactor the code after the code review using best practices in software engineering. For instance, the number of total payment should not be less than zero. Throw an error if this situation happens.

Bu egzersiz

Case Study: Building Software in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Fill in the error condition with the variable representing the total number of months of the loan.
  • Throw a ValueError if the number of monthly payments is less than zero.
  • Return the loan amount.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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 ____
Kodu Düzenle ve Çalıştır