Introduction to Python

Activity 2: Loan Interest Calculator

Lesson 5: Functions in Python

In this activity, we will build a Loan Interest Calculator using user-defined functions. The program will calculate both Simple Interest and Compound Interest based on user input.

Illustration of a bank building with coins, a calculator, and a percentage sign representing a loan interest calculator

Interest Formulas

TypeFormulaVariables
Simple InterestSI = (Principal × Rate × Time) / 100P = Principal, R = Rate %, T = Time in years
Compound InterestCI = P × (1 + R/100)^T - PP = Principal, R = Rate %, T = Time in years
Total Amount (SI)Amount = Principal + SI
Total Amount (CI)Amount = P × (1 + R/100)^T

Writing the Script

# Loan Interest Calculator def simple_interest(principal, rate, time): si = (principal * rate * time) / 100 return si def compound_interest(principal, rate, time): amount = principal * ((1 + rate / 100) ** time) ci = amount - principal return ci # Get inputs principal = float(input("Enter the principal amount: ")) rate = float(input("Enter the interest rate (%): ")) time = float(input("Enter the time period (years): ")) # Calculate si = simple_interest(principal, rate, time) ci = compound_interest(principal, rate, time) # Display results print("--- Results ---") print("Simple Interest: " + str(round(si, 2))) print("Total Amount (SI): " + str(round(principal + si, 2))) print("Compound Interest: " + str(round(ci, 2))) print("Total Amount (CI): " + str(round(principal + ci, 2)))

Testing the Program

  • Run with Principal = 10000, Rate = 5, Time = 3
  • Simple Interest should be 1500.0, Total: 11500.0
  • Compound Interest should be approximately 1576.25, Total: 11576.25
Python console showing the results of the loan interest calculator with simple interest and compound interest results displayed side by side
Save your file with the name Loan Interest Calculator before moving on!

Subscribe to our newsletter.

Get updates to news and events.