Program 3 - Compound interest

Compoind 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 numbers, possibly floating point, 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 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 after the first month would be $10,000+$133.33-$200.00 = $9133.33.

Every month, your program should print out the new balance (after the deduction of interest and the payment).

The last month will require some special handling; the usual monthly payment will 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

Remember, blue is what Python types and black is what the user responds.

Enter amount of debt: 8000
Enter interest rate: 14
Enter monthly payment: 600
Balance = $ 7493
Balance = $ 6980
Balance = $ 6462
Balance = $ 5937
Balance = $ 5406
Balance = $ 4869
Balance = $ 4326
Balance = $ 3777
Balance = $ 3221
Balance = $ 2658
Balance = $ 2089
Balance = $ 1514
Balance = $ 931
Balance = $ 342
Balance = $ 0
Total payments: $ 8746
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 acceptable behaviour (maybe even better...).

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. But for better looking output, you can round everything to the nearest dollar when you print it out, by using the int() function. Or, you can print the floating point numbers with only two decimal places, using a formatting string as described in Lecture on Oct. 13.

You will need a function that checks to see if a string represents a floating point number or not. The isFloat function in the file helper.py does exactly this. In order to have access to this function, you need to put the file helper.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 isFloat() 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 isFloat() function.

Saving and handing in the program

Please name your program yourUID+"3.py", where yourUID is your myUCDavis user ID. For instance, I login to myUCDavis as "amenta", so my program is named "amenta3.py".

Hand in your assignment using myUCDavis. Select ECS 10 from your list of courses on the right. Left-click "Assignments" near the upper left, and then "Project 3". Click on "Drop-Off" and then "Browse". Get to your Desktop, and then to the "ECS 10" folder you made, and finally select your file. The file name should appear in the box next to the "Browse" button. Click "drop-off" to hand in your program. If you are working in the computer classrooms, remember to take your ECS 10 folder, and your program, with you on your flash-drive.