Question
You will write a script to do some useful calculations for fixed-rate mortgages. Your script should have at least the following functions: - get_min_payment() ,
You will write a script to do some useful calculations for fixed-rate mortgages. Your script should have at least the following functions: - get_min_payment() , - interest_due() , - remaining_payments() , - main() , - parse_args()
Script must be in PYTHON. Your script should have at least the following functions:
get_min_payment()
Parameters
the total amount of the mortgage (called the principal; should be a positive number) the annual interest rate (should be a float between 0 and 1) the term of the mortgage, in years (should be a positive integer; default value: 30) the number of payments per year (should be a positive integer; default value: 12)
Functionality
Compute the minimum mortgage payment. Should use the formula A=
Pr(1+r) --------- <-- division ((1+r)^n) 1 where A is the minimum payment amount P is the principal amount r is the interest rate per payment (the annual interest rate divided by the number of payments per year) n is the total number of payments (the term of the mortgage in years the number of payments per year) Use math.ceil() to raise the minimum payment you calculated to the next highest integer, and return this integer. You will need to import math in order to do this. Remember, import statements belong at the top of your script, right after your script docstring.
interest_due()
Parameters the balance of the mortgage (the part of the principal that has not been paid back yet; should be a positive number) the annual interest rate (should be a float between 0 and 1) the number of payments per year (should be a positive integer; default value: 12)
Functionality Compute and return the amount of interest due in the next payment according to the formula i = br, where i is the amount of interest due in the next payment b is the balance of the mortgage r is the interest rate per payment (the annual interest rate divided by the number of payments per year)
remaining_payments()
Parameters the balance of the mortgage (the part of the principal that has not been paid back yet; should be a positive number) the annual interest rate (should be a float between 0 and 1) the payment amount (the amount the user wants to pay per payment; should be a positive number) the number of payments per year (should be a positive integer; default value: 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 broken 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 (i.e., the principal; should be a positive number) the annual interest rate (should be a float between 0 and 1) the term of the mortgage, in years (should be a positive integer; default value: 30) the number of payments per year (should be a positive integer; default value: 12) the user's target payment (the amount the user wishes to pay per payment; should be a positive number or None; default value: 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 user 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 $, you will pay off the mortgage in payments."; replace the angle brackets with the appropriate values)
parse_args()
Parameters
a list of strings containing the command line arguments for the program (when you call this program, you will pass sys.argv[1:] as the argument for this parameter)
Functionality
Create an instance of the ArgumentParser class from the argparse module Use the add_argument() method of your ArgumentParser instance to add the following arguments: Required arguments (i.e., the user can't run the program without specifying these): the total amount of the mortgage (as a float) the annual interest rate (as a float; should be between 0 and 1) Optional arguments (i.e., the user can choose to specify these or omit them and use the default values): the term of the mortgage in years (as an integer; default value: 30) the number of payments per year (as an integer; default value: 12) the target payment amount (as a float; default value: None) Use the parse_args() method of your ArgumentParser instance to parse the list of strings that was passed to your function; this will result in a namespace object, which you should return
Note You will need to import argparse and sys, or at least parts of these modules, in order for this function to work properly. Remember, import statements belong at the top of your script, right after the script docstring.
After your function definitions, you should have an if __name__ == "__main__": statement in which you do the following: pass sys.argv[1:] to parse_args() and store the result in a variable call the main() function; pass in the total amount of the mortgage, the annual interest rate, the term of the mortgage, the number of payments per year, and the target payment amount using the values extracted from the command line arguments by your parse_args() function
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started