Program 3 - Compound interest

Working with a partner

You may work on this assignment with a partner. You and your partner do not have to be in the same section. When working with a partner, you should both sit at the same computer, and agree on what should go into the program. If you let your partner do all the work, not only will it annoy your partner, it will cause you almost certainly to do poorly on the midterm next week. Both of you should hand in the program; make sure both of your names are in a comment on top of the program.

Compound interest

Compound interest is one of the fundamental ideas in finance. Say you have a debt (you're paying interest) or an investment (you're earning interest). At regular intervals, the interest is added to the balance of the debt or investment, increasing the balance and hence the interest due the next time around. If no payments or withdrawls are made to reduce the balance, this leads, slowly but surely, the explosive growth of the balance. For instance, here is a graph showing the growth of a $100 investment earning 7% interest (from a Web site on population growth). The balance doubles every ten years.

Assignment

In this assignment, we will track the balance, month by month, of a debt. This is similar to credit card calculators that can be found on the Web.

The user can enter the initial amount of the debt, the interest rate, and the amount of the monthly payment. All three of these should be floating-point numbers, for instance, the interest rate might be 13.4 or the monthly payment might be 150.00. If the user enters nothing, or something that is not a number, the program should give an error message and exit. Your program should not crash, no matter what input the user gives.

Every month the interest due is calculated and added to the balance, and then the monthly payment is subtraced from the balance. If the size of the monthly payment is big enough to cancel out the interest, then the balance shrinks and the debt is eventually paid off. If not, the balance will increase forever. This would cause the program to go into an infinite loop. Instead of doing that, the program should stop and tell the user that the monthly payment is insufficient.

The interest is compounded monthly; that is, every month, 1/12th of the annual interest rate is paid. So at an interest rate of 16% (typical for a credit card) the monthly interest due on a balance of $10,000 is (16% / 12) * $10,000 = $133.33. If the monthly payment is less than that, the balance will increase forever. If the monthly payment is $200, then the balance going forward after the first month would be $10,000+$133.33-$200.00 = $9133.33. Every month, your program should print out the balance going forward (after the deduction of interest and the payment).

The last month will require some special handling; the usual monthly payment would be more than the current balance plus the interest due in the last month. So the last payment, instead of being the usual amount, should be exactly the remaining balance plus interest.

Example output

Blue is what Python types and black is what the user responds.

Enter amount of debt: 1000
Enter interest rate: 12
Enter monthly payment: 300

Month = 1
Interest this month = 10.00
Balance going forward = 710.00
Total payments = 300.00

Month = 2
Interest this month = 7.10
Balance going forward = 417.10
Total payments = 600.00

Month = 3
Interest this month = 4.17
Balance going forward = 121.27
Total payments = 900.00

Month = 4
Interest this month = 1.21
Balance going forward = 0.00
Total payments = 1022.48

Press enter to exit

Here's another example:
Enter amount of debt: 8000
Enter interest rate: 14.5
Enter monthly payment: 50.00
You will need to make bigger payments.
At this rate you will never pay off the debt.
Press enter to exit
Going back and letting the user enter a larger payment would also be a good way to handle a payment that is too small.

Technical advice

You should use a while loop to iterate through the months. The block inside the loop should contain commands to calculate the interest each month, add it to the balance, and then subtract the payment. There should also be an if statement to check and see if it is the last month, so that that can be handled differently.

For accuracy - to avoid losing money due to round-off - the calculations should be done using floating-point numbers. To get nice looking output, use the format method to show only two decimal places.

You will need a function that checks to see if a string can be converted to a floating point number or not. The canBeFloat function in the file inputCheck.py does exactly this. In order to have access to this function, you need to put the file inputCheck.py in the same folder as your Python program (or have them both on the Desktop). Here is a little example of a program that uses the canBeFloat() function: checkFloats.py. Try putting both .py files in the same folder, and running checkFloats.py, and then look at checkFloats.py, to get an idea of how to use the couldBeFloat() function.

Saving and handing in the program

Hand in your assignment using SmartSite. If you are working with a partner, both of you should hand in the same program. Be sure to include a comment giving both your names and student ID numbers at the top of the program. If you are working in the computer lab, remember to save your program before logging off.