良い変数名を使う
適切な変数命名規則は、可読性と保守性を高め、エラーを減らします。正しい命名規則に従って、クラスと関数に名前を付けてください。
この演習はコースの一部です
ケーススタディ:Pythonでソフトウェアを構築する
演習の手順
- クラスには適切な名前を使いましょう。
mortgage_calculatorとMortgageCalculatorのどちらが正しいでしょうか? - 関数には適切な名前を使いましょう。
calculate_monthly_paymentとCalculateMonthlyPaymentのどちらが正しいでしょうか?
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Use proper naming conventions for a class name
class ____(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)
# Use proper naming conventions for a function name
def ____(self):
numerator = self.monthly_interest_rate * (1 + self.monthly_interest_rate) ** self.months
denominator = (1 + self.monthly_interest_rate) ** self.months - 1
multiplier = self.divide(numerator, denominator)
monthly_payment = round(self.multiply(self.loan_amount, multiplier), 2)
return monthly_payment