If you’ve ever compared the effect of slightly different interest rates over the long term, you’ll agree with Albert Einstein that “Compound interest is the eighth wonder of the world.”
Until recently, I had experimented with compound interest using websites and excel. But after using Python, I realised it would be superior to use for the following reasons:
Python is better for delivery of an output from an input, via a terminal like the one you get in Visual Studio. This way, you put a few numbers in, and a user-friendly result comes out. This makes it easily repeatable.
With well written code and comments, it is easy to understand and interrogate the calculation.
With an alternative like Excel, you may have to dig into the cell or read an adjacent line of text. With an app or a website, you run on trust since you are unable to interrogate the inner workings.
This blog details the considerations and building process. The final code is available on GitHub and at the end of this blog.
What does the code need to do?
- Run a simple algebraic function using inputted variables
- Successfully apply interest rate. E.g. 5% turns £1 into £1.05, not £5 or £0.05
- Provide an easy User Interface (UI) that anyone can use
Function
In pseudocode:
Final sum = initial value x (interest rate x number of years)
In Python:
# This function calculates an output based on initial value /
# Interest rate and number of years. These are all to be inputted by the user.
def compound_interest(initial_value, interest_rate, years):
output = initial_value*(1 + interest_rate)**years
return output
Inputs
Now I need to define the variables: initial_value, interest_rate, years as all user-inputs, so that when we run the code, that is what the user is asked for.
initial_value = float(input("Enter the initial value without currency or commas e.g. 1000 or 1000.99:"))
interest_rate = float(input("Enter the interest rate, as a decimal, e.g. 5% = 0.05:"))
years = int(input("Enter the number of years:"))
#Initial value is best used as a float, so you can use decimal values like £120.52
#Interest rate is also best used as a float, for functional reasons
The input() functions ensure the user will be prompted to enter a value, and the strings in quotation marks tell the user how to do so.
Testing
Before improving the UI, I decided to test that this code actually works.
Running this code in the terminal we get the first input prompt:
“Enter the initial value:”
We type a figure. In this test we’ll use a float, because why go easy on it with an integer?
We type “15420.99” and hit enter.
Then we get the second input prompt:
“Enter the interest rate, as a decimal, e.g. 5% = 0.05:”
We type a figure. In this case we’ll use 8.5% or 0.085. Again, why be nice and use the example?
Then we get the third input prompt:
“Enter the number of years:”
We type 5, press enter, and the final value is given as:
23187.87478266996
Here’s how it looks in the terminal on completion:
Enter the initial value:15420.99
Enter the interest rate, as a decimal, e.g. 5% = 0.05:0.085
Enter the number of years:5
23187.87478266996
Did it work?
Let’s compare it to a compound interest calculator I found online: https://www.thecalculatorsite.com/finance/calculators/compoundinterestcalculator.php
Answer: £23,187.87
It works! 🙂
Ideas and improvements:
The function would still work if years had the float data type rather than integer. This way you could enter 6.5 for 6 and a half years, etc. I confirmed with a test that it works, but returned the code to its simplest integer form.
You don’t usually denote money with 11 decimal places… Using the round function I created the new output, adding an extra line to the function as so:
def compound_interest(initial_value, interest_rate, years):
output = initial_value*(1 + interest_rate)**years
rounded_output = round(output, 2)
return rounded_output
Result: 23187.87
We can add some words and currency to make it look nice, by modifying the print() function to precede the output with words and a currency symbol
print("Initial + interest = £",compound_interest(initial_value, interest_rate, years))
Result: Initial + interest = £ 23187.87
Code (final):
# This function calculates an output based on initial value, interest rate, and number of years.
def compound_interest(initial_value, interest_rate, years):
output = initial_value*(1 + interest_rate)**years
rounded_output = round(output, 2)
return rounded_output
initial_value = float(input("Enter the initial value:"))
interest_rate = float(
input("Enter the interest rate, as a decimal, e.g. 5% = 0.05:"))
years = float(input("Enter the number of years:"))
#Initial value is best used as a float, so you can use decimal values like £120.52
#Interest rate is also best used as a float, for the function to work
print("Initial + interest = £",compound_interest(initial_value, interest_rate, years))
Terminal output (example):
Enter the initial value:15420.99
Enter the interest rate, as a decimal, e.g. 5% = 0.05:0.085
Enter the number of years:5
Initial + interest = £ 23187.87
Reflections:
I believe there is room to improve the UI. When I improve my skills I may return to this exercise and improve it; after all this is just project number 1!
Compound interest is usually done at annual intervals, as in this case, but it could easily be done with months, weeks, or days
AI tools like ChatGPT / Copilot were not used to make anything in this exercise. They would have saved time but would’ve deprived me of the learning experience and satisfaction. I did run the final code through ChatGPT at the end to see if I missed anything, and it was as agreeable as always. Crucially, if there’s any error here, it’s 100% human.
Top comments (0)