Lumpsum Investment Calculator in Python
A lumpsum investment calculator helps investors determine the future value of a one-time investment based on an expected rate of return and investment period. In this tutorial, we will create a simple lumpsum investment calculator in Python and extend it to an advanced version with a graphical user interface (GUI) using Tkinter. Stay with me and this is definitely going to be an interesting project to brush up your Python skills.
Formula for Lumpsum Investment
The formula to calculate the future value of a lumpsum investment is:
FV = PV × (1 + (r / 100))^n
Where:
- FV is the future value of the investment.
- PV is the principal value or initial investment amount.
- r is the annual rate of return (in percentage).
- n is the number of years the money is invested.
Simple Lumpsum Investment Calculator
First, we will try creating a simplier version of the Lumpsum Investment Calculator. It will be CLI (Command Line Interface) based.
Step 1: Define the Calculation Function
Let’s start by defining a function to calculate the future value of a lumpsum investment. This function is used to calculate the actual future value, FV of the investment, by taking PV (Principal value), r (rate), n (number of years) as input.
def calculate_lumpsum(pv, r, n): fv = pv * (1 + r / 100) ** n return fv
Step 2: Get User Input
We will prompt the user to enter the initial investment amount, annual rate of return, and investment period.
pv = float(input("Enter the initial investment amount (PV): ")) r = float(input("Enter the annual rate of return (r) in percentage: ")) n = int(input("Enter the investment period (n) in years: "))
Step 3: Calculate and Display the Future Value
We will use the `calculate_lumpsum` function to compute the future value and display the result.
fv = calculate_lumpsum(pv, r, n) print(f"The future value of the investment is: {fv:.2f}")
Complete Code
Here is the complete code for the simple lumpsum investment calculator.
def calculate_lumpsum(pv, r, n): fv = pv * (1 + r / 100) ** n return fv pv = float(input("Enter the initial investment amount (PV): ")) r = float(input("Enter the annual rate of return (r) in percentage: ")) n = int(input("Enter the investment period (n) in years: ")) fv = calculate_lumpsum(pv, r, n) print(f"The future value of the investment is: {fv:.2f}")
Output
Here is a sample output.
Enter the initial investment amount: 10000 Enter the annual interest rate (in %): 12 Enter the number of years: 10 The future value of the investment after 10 years is: ₹31058.48
Advanced Lumpsum Investment Calculator with Tkinter
Now, let’s create an advanced version of the lumpsum investment calculator with a graphical user interface (GUI) using Tkinter.
Step 1: Install Tkinter
First, go to the project directory and open the terminal by clicking ctrl + `
Then type pip install Tkinter
in the terminal to install the required module.
Step 2: Import Tkinter
Then, import the Tkinter module.
import tkinter as tk from tkinter import messagebox
Step 3: Define the Calculation Function
We’ll use the same `calculate_lumpsum` function from the simple version.
def calculate_lumpsum(pv, r, n): fv = pv * (1 + r / 100) ** n return fv
Step 4: Create the GUI
We’ll create a simple GUI with input fields for the initial investment amount, annual rate of return, and investment period. We’ll also add a button to calculate the future value and display the result.
-
Main Window Creation:
root = tk.Tk() root.title("Lumpsum Investment Calculator")
–
tk.Tk()
: Creates the main window of the application.
–root.title("Lumpsum Investment Calculator")
: Sets the title of the window. -
Input Fields and Labels:
tk.Label(root, text="Initial Investment Amount (PV):").grid(row=0, column=0, padx=10, pady=10) entry_pv = tk.Entry(root) entry_pv.grid(row=0, column=1, padx=10, pady=10) tk.Label(root, text="Annual Rate of Return (r) in %:").grid(row=1, column=0, padx=10, pady=10) entry_r = tk.Entry(root) entry_r.grid(row=1, column=1, padx=10, pady=10) tk.Label(root, text="Investment Period (n) in years:").grid(row=2, column=0, padx=10, pady=10) entry_n = tk.Entry(root) entry_n.grid(row=2, column=1, padx=10, pady=10)
–
tk.Label
: Creates labels for each input field.
–tk.Entry
: Creates entry widgets for users to input data. -
Calculate Button:
calculate_button = tk.Button(root, text="Calculate", command=calculate_and_display) calculate_button.grid(row=3, column=0, columnspan=2, pady=10)
–
tk.Button
: Creates a button widget.
–command=calculate_and_display
: Specifies the function to call when the button is clicked. -
Result Label:
result_label = tk.Label(root, text="") result_label.grid(row=4, column=0, columnspan=2, pady=10)
–
tk.Label
: Creates a label widget to display the calculated result. -
Main Loop:
root.mainloop()
–
root.mainloop()
: Starts the Tkinter event loop, allowing the GUI to handle user inputs and events.
Complete Code with GUI
Here is the complete code for the advanced lumpsum investment calculator with a graphical user interface using Tkinter.
import tkinter as tk from tkinter import messagebox def calculate_lumpsum(pv, r, n): fv = pv * (1 + r / 100) ** n return fv def calculate_and_display(): try: pv = float(entry_pv.get()) r = float(entry_r.get()) n = int(entry_n.get()) fv = calculate_lumpsum(pv, r, n) result_label.config(text=f"The future value of the investment is: {fv:.2f}") except ValueError: messagebox.showerror("Input Error", "Please enter valid numbers") # Create the main window root = tk.Tk() root.title("Lumpsum Investment Calculator") # Create and place the input fields and labels tk.Label(root, text="Initial Investment Amount (PV):").grid(row=0, column=0, padx=10, pady=10) entry_pv = tk.Entry(root) entry_pv.grid(row=0, column=1, padx=10, pady=10) tk.Label(root, text="Annual Rate of Return (r) in %:").grid(row=1, column=0, padx=10, pady=10) entry_r = tk.Entry(root) entry_r.grid(row=1, column=1, padx=10, pady=10) tk.Label(root, text="Investment Period (n) in years:").grid(row=2, column=0, padx=10, pady=10) entry_n = tk.Entry(root) entry_n.grid(row=2, column=1, padx=10, pady=10) # Create and place the Calculate button calculate_button = tk.Button(root, text="Calculate", command=calculate_and_display) calculate_button.grid(row=3, column=0, columnspan=2, pady=10) # Create and place the result label result_label = tk.Label(root, text="") result_label.grid(row=4, column=0, columnspan=2, pady=10) # Run the main loop root.mainloop()
Output
Here is a sample output.
https://drive.google.com/file/d/1wXugkDXTOqnw8irYf_MpxZikFpaCQjFu/view
https://drive.google.com/file/d/1i2feNpnbR9guzcrNl6JY-9FJuNsT2mZc/view
Conclusion
In this tutorial, we created both a simple and an advanced version of a lumpsum investment calculator in Python. The simple version uses basic input and output functions, while the advanced version features a graphical user interface using Tkinter. By following these steps, you can easily create and customize your own investment calculator to suit your specific needs.