Description
Finally, let's add to our calculator the capacity to compute differentiated payments. We’ll do this for the type of repayment where the loan principal is reduced by a constant amount each month. The rest of the monthly payment goes toward interest repayment and it is gradually reduced over the term of the loan. This means that the payment is different each month. Let’s look at the formula:
Where:
= th differentiated payment;
= the loan principal;
= nominal interest rate. This is usually 1/12 of the annual interest rate and is a float value, not a percentage. For example, if our annual interest rate = 12%, then i = 0.01.
= number of payments. This is usually the number of months in which repayments will be made.
= current repayment month.
In this stage, the user has to provide more inputs, so your program should be able to parse new command-line arguments. Let's add the --type one, so now the calculator should be run with 4 arguments:
python creditcalc.py --type=diff --principal=1000000 --periods=10 --interest=10The --type argument is indicating the type of payment: "annuity" or "diff" (differentiated). It must always be provided in any run.
Also, it is possible that the user will make a mistake in the provided input. The program should not fail, but inform the user, that he has provided "Incorrect parameters".
--typeis specified neither as"annuity"nor as"diff"or not specified at all:> python creditcalc.py --principal=1000000 --periods=60 --interest=10 Incorrect parametersFor
--type=diff, the payment is different each month, so we can't calculate months or principal, therefore a combination with--paymentis invalid:> python creditcalc.py --type=diff --principal=1000000 --interest=10 --payment=100000 Incorrect parametersOur loan calculator can't calculate the interest, so it must always be provided:
> python creditcalc.py --type=annuity --principal=100000 --payment=10400 --periods=8 Incorrect parametersFor differentiated payments you will need 4 out of 5 parameters (excluding payment), and the same is true for annuity payments (the user will be calculating the number of payments, the payment amount, or the loan principal). Thus, you should also display an error message when fewer than four parameters are provided:
> python creditcalc.py --type=annuity --principal=1000000 --payment=104000 Incorrect parametersNegative values should not be provided:
> python creditcalc.py --type=diff --principal=30000 --periods=-14 --interest=10 Incorrect parameters
Objectives
In this stage, you are going to implement the following features:
Calculation of differentiated payments. To do this, the user can run the program specifying interest, number of monthly payments, and loan principal.
Ability to calculate the same values as in the previous stage for annuity payment (principal, number of monthly payments, and monthly payment amount).
Handling of invalid parameters. It's a good idea to show an error message if the user enters invalid parameters.
The only thing left is to compute the overpayment: the amount of interest paid over the whole term of the loan.
Examples
The greater-than symbol followed by a space (> ) represents the user input. Note that this is not part of the input.
Example 1: calculating differentiated payments
> python creditcalc.py --type=diff --principal=1000000 --periods=10 --interest=10
Month 1: payment is 108334
Month 2: payment is 107500
Month 3: payment is 106667
Month 4: payment is 105834
Month 5: payment is 105000
Month 6: payment is 104167
Month 7: payment is 103334
Month 8: payment is 102500
Month 9: payment is 101667
Month 10: payment is 100834
Overpayment = 45837In this example, the user wants to take a loan with differentiated payments. You know the principal, the count of periods, and interest, which are 1,000,000, 10 months, and 10%, respectively.
The calculator should calculate payments for all 10 months. Let’s look at the formula above. In this case:
Now let’s calculate the payment for the first month:
The second month ():
The third month ():
And so on. You can see other monthly payments above.
Your loan calculator should output monthly payments for every month as in the first stage. Also, round up all floating-point values.
Finally, your loan calculator should add up all the payments and subtract the loan principal so that you get the overpayment.
Example 2: calculate the annuity payment for a 60-month (5-year) loan with a principal amount of 1,000,000 at 10% interest
> python creditcalc.py --type=annuity --principal=1000000 --periods=60 --interest=10
Your annuity payment = 21248!
Overpayment = 274880Example 3: fewer than four arguments are given
> python creditcalc.py --type=diff --principal=1000000 --payment=104000
Incorrect parameters.Example 4: calculate differentiated payments given a principal of 500,000 over 8 months at an interest rate of 7.8%
> python creditcalc.py --type=diff --principal=500000 --periods=8 --interest=7.8
Month 1: payment is 65750
Month 2: payment is 65344
Month 3: payment is 64938
Month 4: payment is 64532
Month 5: payment is 64125
Month 6: payment is 63719
Month 7: payment is 63313
Month 8: payment is 62907
Overpayment = 14628Example 5: calculate the principal for a user paying 8,722 per month for 120 months (10 years) at 5.6% interest
> python creditcalc.py --type=annuity --payment=8722 --periods=120 --interest=5.6
Your loan principal = 800018!
Overpayment = 246622Example 6: calculate how long it will take to repay a loan with 500,000 principal, monthly payment of 23,000, and 7.8% interest
> python creditcalc.py --type=annuity --principal=500000 --payment=23000 --interest=7.8
It will take 2 years to repay this loan!
Overpayment = 52000