# Mortgage Calculations Table of Contents Instructions · get_min_payment() · interest_due() · remaining_payments() · main() · Template · Running your program · Testing Instructions For this homework you...

Mortgage Calculations
Instructions
· get_min_payment()
· interest_due()
· remaining_payments()
· main()
· Template
· Testing
Instructions
For this homework you will write a script to do some useful calculations for fixed-rate mortgages. Use the template provided below. At a minimum, your script should contain the functions get_min_payment(), interest_due(), remaining_payments(), and main(), each of which is described below. Be sure to include a docstring in each function.
get_min_payment()
Parameters
· The total amount of the mortgage (called the principal; you can assume this is a positive number)
· The annual interest rate (you can assume this is a float between 0 and 1; for example, an interest rate of 3.5% would be expressed as 0.035)
· The term of the mortgage in years (you can assume this is a positive integer; assign this parameter a default value of 30)
· The number of payments per year (you can assume this is a positive integer; assign this parameter a default value of 12)
Functionality
1. Compute the minimum mortgage payment (AA). You should use the formula A=Pr(1+r)n(1+r)n−1A=Pr(1+r)n(1+r)n-1 where
· PP is the principal amount
·
is the interest rate per payment (the annual interest rate divided by the number of payments per year)
· nn is the total number of payments (the term of the mortgage in years × the number of payments per year)
2. Use math.ceil() to raise the minimum payment you calculated in step 1 to the next highest integer; return this integer.
· You will need to import math in order to do this. import statements belong at the top of your script, right after your script docstring.
NOTE
This function takes into account only principal and interest, which are typically the two largest components in a mortgage payment. In the real world, mortgage payments often also cover other expenses like property taxes, home insurance, and private mortgage insurance.
interest_due()
Parameters
· The balance of the mortgage (the part of the principal that has not been paid back yet; you can assume this is a positive number)
· The annual interest rate (you can assume this is a float between 0 and 1; for example, an interest rate of 3.5% would be expressed as 0.035)
· The number of payments per year (you can assume this is a positive integer; assign this parameter a default value of 12)
Functionality
· Compute and return ii, the amount of interest due in the next payment according to the formula i=
i=
, where
·
is the the balance of the mortgage
·
is the interest rate per payment (the annual interest rate divided by the number of payments per year)
emaining_payments()
Parameters
· The balance of the mortgage (the part of the principal that has not been paid back yet; you can assume this is a positive number)
· The annual interest rate (you can assume this is a float between 0 and 1; for example, an interest rate of 3.5% would be expressed as 0.035)
· The target payment (the amount the user wants to pay per payment; you can assume this is a positive number)
· The number of payments per year (you can assume this is a positive integer; assign this parameter a default value of 12)
Functionality
Compute and return the number of payments required to pay off the mortgage. We will do this by simulating payments one at a time until the balance of the mortgage reaches zero, assuming fixed payments (in other words, assume that each payment is the same amount of money as the previous one).
Note that a mortgage payment is
oken down into two parts: an interest payment, and a part that pays down the balance of the mortgage. The interest payment is calculated as described under interest_due(); the rest of the payment goes toward the balance of the mortgage. The percentage of the payment that goes toward interest decreases with every payment because it depends on the balance of the mortgage, which also decreases with every payment.
Here is an algorithm for simulating payments until the mortgage is paid off:
· Initialize a counter with a value of zero; this counter represents the number of payments to be made.
· As long as the balance of the mortgage is positive, do the following:
· Use the interest_due() function to determine what portion of the next payment will be interest. The total payment minus interest due is the amount that will go toward paying the balance of the principal. Remember, these amounts will change with every payment.
· Reduce the balance by the part of the payment that goes toward paying the balance.
· Increase the counter.
· When the balance of the mortgage is no longer positive, the value of the counter is the number of payments required. Return this value.
main()
Parameters
· The total amount of the mortgage (called the principal; you can assume this is a positive number)
· The annual interest rate (you can assume this is a float between 0 and 1; for example, an interest rate of 3.5% would be expressed as 0.035)
· The term of the mortgage in years (you can assume this is a positive integer; assign this parameter a default value of 30)
· The number of payments per year (you can assume this is a positive integer; assign this parameter a default value of 12)
· The user’s target payment (the amount the user wishes to pay per payment; you can assume this is a positive number or None; assign this parameter a default value of None)
· A value of None indicates that the user wishes to use the minimum payment
Functionality
· Compute the minimum payment using the get_min_payment() function
· Display the minimum payment to the use
· If the user’s target payment is None, set the target payment to the minimum payment
· If the user’s target payment is less than the minimum payment, print a message to the user (e.g., "Your target payment is less than the minimum payment for this mortgage"); otherwise:
· Use remaining_payments() to figure out the total number of payments required (hint: at the beginning of a mortgage, the balance is equal to the total mortgage amount)
· Display this number to the user (e.g., "If you make payments of \$< target payment >, you will pay off the mortgage in < total payments > payments."; replace the angle
ackets with the appropriate values)
Template
"""Perform fixed-rate mortgage calculations."""
from argparse import ArgumentParse
import math
import sys
# replace this comment with your implementation of get_min_payment(),
# interest_due(), remaining_payments(), and main()
def parse_args(arglist):
"""Parse and validate command-line arguments.
Args:
XXXXXXXXXXarglist (list of str): list of command-line arguments.
Returns:
XXXXXXXXXXnamespace: the parsed arguments (see argparse documentation fo
Raises:
XXXXXXXXXXValueE
or: encountered an invalid argument.
"""
# set up argument parse
parser = ArgumentParser()
XXXXXXXXXXhelp="the total amount of the mortgage")
XXXXXXXXXXhelp="the annual interest rate, as a float"
" between 0 and 1")
XXXXXXXXXXhelp="the term of the mortgage in years (default: 30)")
XXXXXXXXXXhelp="the number of payments per year (default: 12)")
XXXXXXXXXXhelp="the amount you want to pay per payment"
" (default: the minimum payment)")
# parse and validate arguments
args = parser.parse_args()
if args.mortgage_amount < 0:
XXXXXXXXXXraise ValueE
or("mortgage amount must be positive")
if not 0 <= args.annual_interest_rate <= 1:
XXXXXXXXXXraise ValueE
or("annual interest rate must be between 0 and 1")
if args.years < 1:
XXXXXXXXXXraise ValueE
or("years must be positive")
if args.num_annual_payments < 0:
XXXXXXXXXXraise ValueE
or("number of payments per year must be positive")
if args.target_payment and args.target_payment < 0:
XXXXXXXXXXraise ValueE
or("target payment must be positive")
return args
if __name__ == "__main__":
try:
XXXXXXXXXXargs = parse_args(sys.argv[1:])
except ValueE
or as e:
XXXXXXXXXXsys.exit(str(e))
main(args.mortgage_amount, args.annual_interest_rate, args.years,
XXXXXXXXXXargs.num_annual_payments, args.target_payment)
Your program is designed to run from the terminal. To run it, open a terminal and ensure you are in the directory where your script is saved.
The program takes two required command-line arguments: a mortgage amount and an interest rate (expressed as a float between 0 and 1). It also allows the following optional arguments: -y (the term of the mortgage in years), -n (the number of annual payments), and -p (the target payment). Below are some examples of how to use the program. The examples assume you are using macOS and your program is called mortgage.py. If you are using Windows, replace python3 with python. If your program has a different name, replace mortgage.py with the name of your program.
Basic usage
python3 mortgage.py XXXXXXXXXX
With one optional paramete
python3 mortgage.py XXXXXXXXXXy 15
With multiple optional parameters
python3 mortgage.py XXXXXXXXXXy 15 -p 4000
Testing
You can use the script test_mortgage.py to test your solution. Make sure you have Pytest installed (see https:
docs.pytest.org/en/latest/getting-started.html#install-pytest (Links to an external site.)) and that your script and test_mortgage.py are in the same directory on your computer. Edit line 4 of test_mortgage.py so that it contains the name of your script (withou the .py ending).
To use the test script, open a terminal and navigate to the directory containing your script and test_mortgage.py. Then type the following at the command prompt:
pytest test_mortgage.py
Answered 3 days AfterApr 07, 2022

## Solution

Neetha answered on Apr 09 2022
SOLUTION.PDF