Custom Class
1. Custom Class
A new sprint at PyBank is starting.2. Roadmap
Now that you've added documentation to your class and explored keeping track of inherited methods, you will add some functionality to the mortgage calculator.3. Monthly Mortgage Calculation
As a loan officer at PyBank, you know lots of numbers people. You ask your colleague for the formula used to calculate the monthly payment of a mortgage given the loan amount, the monthly interest rate, and the number of payments. (Note, it is not necessary to understand the details of the formula to write the code.) Think of the right-hand side of the equation as the product of the loan amount and a term called the "multiplier." The multiplier's numerator is the monthly interest rate times (1 + interest rate) raised to the power of the number of payments. The denominator is (1 + interest rate) raised to the power of the number of payments, minus one.4. Add Functionality to Our Class
You decide to simplify the formula by breaking it into steps. First, you compute the multiplier by dividing a numerator by a denominator, using the `divide()` method from the `BasicCalculator` class. You then calculate the monthly payment by multiplying the loan amount with this multiplier using the `multiply()` method and return the monthly payment result.5. Compute the Monthly Payment
As an example of computing the monthly payment, consider a mortgage with a loan amount of $300,000, a yearly interest rate of 6.5% and a loan term of 30 years. Instantiate the class using these values. Then, print the `monthly_payment` attribute of `mortgage_calculator`.6. dir() again
Recall, the `dir()` function in Python lists all the attributes and methods of an object, letting you see what you can do with it. For example, `dir(my_class)` shows all the methods available in `my_class`. Your new method should now appear in a `dir()` command. In a terminal, use `dir(ClassName)` to get a list of the methods of a class `ClassName`. When you apply the `dir()` command to the `MortgageCalculator` class, the new method you wrote is listed near the bottom of the list.7. F-string Example
Suppose you want to introduce the name of a friend and her age using the sentence "Hello, my name is Alica and I'm 17 years old." First, you store the name and age in two variables, `name` and `age`. Next, you create an f-string an assign it to `greeting`. In this line, `{name}` will be replaced by the value of the `name` variable, which is "Alice", and `{age}` will be replaced by the value of the `age` variable, which is 17. When you print `greeting`, it displays, "Hello, my name is Alice and I'm 17 years old."8. Let's practice!
Congrats on learning about the mortgage calculator! Now go code!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.