Math / Python / Q&A · May 21, 2023 0

Python Simple and Compound Interest Calculator

Don’t have a calculator? Don’t worry. Just paste this code into Python and let Python do all the work.

from math import log
compound_or_simple = input("simple or compound interest? (s/c)")
if compound_or_simple == "s":
    what_to_find = input("Do you want to calculate time, interest, rate or principal? (t/i/r/p) ")
    if what_to_find == "t":
        i = float(input("How much interest has been earned in dollars?  "))
        r = float(input("What is the rate of interest as a percentage?  "))/100
        p = float(input("What was the principal?  "))
        time = i/r/p
        print("The investment was %.2f years." % time)
    elif what_to_find == "i":
        r = float(input("What is the rate of interest as a percentage?  "))/100
        p = float(input("What was the principal?  "))
        t = float(input("How long was the investment in years?  "))
        interest = r*p*t
        print("The investment earned $%.2f in interest." % interest)
    elif what_to_find == "r":
        p = float(input("What was the principal?  "))
        t = float(input("How long was the investment in years?  "))
        i = float(input("How much interest has been earned in dollars?  "))
        rate = i/p/t
        print("The investment rate of interest was %.2f percent." % (rate*100))
    elif what_to_find == "p":
        r = float(input("What is the rate of interest as a percentage?  "))/100
        i = float(input("How much interest has been earned in dollars?  "))
        t = float(input("How long was the investment in years?  "))
        principal = i/r/t
        print("The amount of money originally invested was $%.2f." % principal)
    else:
        print("Unrecognized input, try again!")
elif compound_or_simple == "c":
    what_to_find = input("Do you want to calculate time, interest, rate or principal? (t/i/r/p) ")
    n = int(input("How many compounding periods per year?  "))
    if what_to_find == "t":
        i = float(input("How much interest has been earned in dollars?  "))
        r = float(input("What is the rate of interest as a percentage?  "))/100
        p = float(input("What was the principal?  "))
        time = log((p+i)/p)/(n * log(1 + r/n))
        print("The investment was %.2f years." % time)
    elif what_to_find == "i":
        r = float(input("What is the rate of interest as a percentage?  "))/100
        p = float(input("What was the principal?  "))
        t = float(input("How long was the investment in years?  "))
        interest = p*((1 + r/n)**(n*t) - 1)
        print("The investment earned $%.2f in interest." % interest)
    elif what_to_find == "r":
        p = float(input("What was the principal?  "))
        t = float(input("How long was the investment in years?  "))
        i = float(input("How much interest has been earned in dollars?  "))
        rate = n*(((p+i)/p)**(1/(n*t)) - 1)
        print("The investment rate of interest was %.2f percent." % rate)
    elif what_to_find == "p":
        r = float(input("What is the rate of interest as a percentage?  "))/100
        i = float(input("How much interest has been earned in dollars?  "))
        t = float(input("How long was the investment in years?  "))
        principal = i / ((1 + r/n)**(n*t) - 1)
        print("The amount of money originally invested was $%.2f." % principal)
    else:
        print("Unrecognized input, try again!")
else:
    print("Unrecognized input, try again!")