使用良好的變數命名
良好的變數命名慣例能提升可讀性、強化可維護性,並降低錯誤。請依照正確的命名慣例,為一個類別與一個函式命名。
本練習屬於課程
案例研究:用 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