开始使用免费开始使用

使用良好的变量命名

恰当的变量命名规范可以提升可读性、增强可维护性,并减少错误。请按照命名规范为一个类和一个函数命名。

本练习是课程的一部分

案例研究:用 Python 构建软件

查看课程

练习说明

  • 为类使用合适的名称。在 mortgage_calculatorMortgageCalculator 中选择?
  • 为函数使用合适的名称。在 calculate_monthly_paymentCalculateMonthlyPayment 中选择?

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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
编辑并运行代码