कोड को Refactor करें
आपने कोड रिव्यू के बाद, सॉफ्टवेयर इंजीनियरिंग की best practices अपनाते हुए कोड को refactor करने का निर्णय लिया है. उदाहरण के लिए, कुल भुगतान (total payments) की संख्या शून्य से कम नहीं होनी चाहिए. अगर ऐसा हो, तो एक error उठाएँ.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडी: Python में सॉफ्टवेयर बनाना
अभ्यास निर्देश
- लोन के कुल महीनों का प्रतिनिधित्व करने वाले वैरिएबल के साथ error की शर्त भरें.
- अगर मासिक भुगतानों की संख्या शून्य से कम हो, तो
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 ____