Python code for Financial Calculator

In this post, we will create a financial calculator in Python that will calculate simple and compound interest. Unlike the previous example, this calculator will not have a graphical user interface (GUI). Instead, it will be a console application that takes user input from the keyboard and prints the results to the console.

First, let’s create a function to calculate simple interest. Simple interest is calculated using the formula I = P * r * t, where P is the principal amount, r is the interest rate, and t is the time period.

def simple_interest(principal, rate, time):
    interest = principal * rate * time
    return interest

In this code, we define a function simple_interest() that takes three parameters: principal, rate, and time. The function calculates the interest using the formula and returns the result.

Next, let’s create a function to calculate compound interest. Compound interest is calculated using the formula A = P * (1 + r/n)^(n*t), where A is the total amount, P is the principal amount, r is the interest rate, t is the time period, and n is the number of times interest is compounded per year.

def compound_interest(principal, rate, time, compound):
    amount = principal * (1 + (rate/compound))**(compound*time)
    return amount

In this code, we define a function compound_interest() that takes four parameters: principal, rate, time, and compound. The function calculates the amount using the formula and returns the result.

Now, let’s create the main program that will take user input from the keyboard and call the appropriate function based on the user’s choice.

print("Welcome to the Financial Calculator!")
print("Please choose an option:")
print("1. Simple Interest")
print("2. Compound Interest")

choice = int(input("Enter your choice (1 or 2): "))

if choice == 1:
    principal = float(input("Enter the principal amount: "))
    rate = float(input("Enter the interest rate (as a decimal): "))
    time = float(input("Enter the time period (in years): "))
    interest = simple_interest(principal, rate, time)
    print("The simple interest is: $", interest)

elif choice == 2:
    principal = float(input("Enter the principal amount: "))
    rate = float(input("Enter the interest rate (as a decimal): "))
    time = float(input("Enter the time period (in years): "))
    compound = float(input("Enter the compounding frequency per year: "))
    amount = compound_interest(principal, rate, time, compound)
    print("The compound interest is: $", amount)

else:
    print("Invalid choice. Please enter either 1 or 2.")

In this code, we first print a welcome message and a menu of options for the user to choose from. We then read the user’s choice from the keyboard and call the appropriate function based on the choice. If the user chooses option 1, we prompt for the principal amount, interest rate, and time period, and call the simple_interest() function to calculate the interest. If the user chooses option 2, we prompt for the principal amount, interest rate, time period, and compounding frequency, and call the compound_interest() function to calculate the amount. Finally, we print the result to the console.

That’s it! You have now created a simple financial calculator in Python that can calculate simple and compound interest. This example can be expanded to include other financial calculations such as amortization, present value, and future value.