始める無料で始める

コードのリファクタリング

コードレビューの結果に基づき、ソフトウェアエンジニアリングのベストプラクティスに沿ってコードをリファクタリングします。たとえば、支払い総回数はゼロ未満であってはなりません。この状況が発生したらエラーを投げてください。

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

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

コースを見る

演習の手順

  • 返済総月数を表す変数を使って、エラー条件を記述してください。
  • 毎月の支払回数がゼロ未満の場合は 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 ____
コードを編集して実行