Simple Interest Calculator in Python.
About the project: This will be a self-contained script that calculates simple interest based on the principal amount, interest rate, and time period.
It will run in a continuous loop, so you can perform multiple conversions without restarting the program.
This script provides a user-friendly interface to calculate simple interest.
How to use this Simple Interest Calculator:
- Run the Python script.
- When you run it, It prompts you for the principal amount, interest rate, and time period,
- then outputs a summary of the results.
- You can perform multiple calculations in the same session.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# simple_interest_calculator.py
def get_numeric_input(prompt, min_value=0):
"""
Helper function to get valid numeric input from the user.
Ensures the input is a positive number.
"""
while True:
try:
value = float(input(prompt).strip())
if value <= min_value:
print("Please enter a positive value.")
else:
return value
except ValueError:
print("Invalid input. Please enter a number.")
def calculate_simple_interest(principal, rate, time):
"""
Calculates the simple interest and the total amount.
Formula: Simple Interest = (Principal * Rate * Time) / 100
Args:
principal (float): The initial amount of money.
rate (float): The annual interest rate (in percent).
time (float): The time the money is invested or borrowed for (in years).
Returns:
tuple: A tuple containing the calculated simple interest and the total amount.
"""
interest = (principal * rate * time) / 100
total_amount = principal + interest
return interest, total_amount
def main():
"""
Main function to run the Simple Interest Calculator in a continuous loop.
"""
print("--- Python Simple Interest Calculator ---")
print("Enter 'q' at any time to quit.")
while True:
try:
principal = get_numeric_input("Enter the principal amount: ")
rate = get_numeric_input("Enter the annual interest rate (%): ")
time = get_numeric_input("Enter the time in years: ")
interest, total = calculate_simple_interest(principal, rate, time)
print("\n--- Calculation Summary ---")
print(f"Principal Amount: ${principal:.2f}")
print(f"Annual Rate: {rate}%")
print(f"Time Period: {time} years")
print(f"Simple Interest: ${interest:.2f}")
print(f"Total Amount: ${total:.2f}")
print("---------------------------\n")
play_again = input("Do you want to perform another calculation? (yes/no): ").strip().lower()
if play_again != 'yes':
print("Exiting Simple Interest Calculator. Goodbye!")
break
except ValueError:
print("Invalid input. Please enter a valid number.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()