Bắt đầu ngayBắt đầu miễn phí

Đặt tên biến tốt

Quy ước đặt tên biến đúng giúp mã dễ đọc, dễ bảo trì và giảm lỗi. Hãy áp dụng quy ước đặt tên phù hợp để đặt tên cho một lớp và một hàm.

Bài tập này là một phần của khóa học

Nghiên cứu tình huống: Xây dựng phần mềm bằng Python

Xem khóa học

Hướng dẫn bài tập

  • Dùng tên phù hợp cho lớp. Chọn mortgage_calculator hay MortgageCalculator?
  • Dùng tên phù hợp cho hàm. Chọn calculate_monthly_payment hay CalculateMonthlyPayment?

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# 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
Chỉnh sửa và Chạy Mã