Building a Compound Interest Calculator

Aug 10, 2024

Compound Interest Calculator in Python

Introduction

  • Objective: Create a compound interest calculator in Python.
  • Definition of Interest: Charge for borrowing money; accrual of interest in bank accounts.

Key Variables

  1. Principal: Initial investment amount.
  2. Rate: Rate of interest (as a percentage).
  3. Time: Duration in years for which the interest is calculated.

Program Structure

  • Use of while loops to validate user input.

Input Validation

  • Principal Input:

    • Prompt user to input principal amount.
    • Use while loop: while principal <= 0
    • Typecast input to float.
    • Inform user if the principal is invalid.
  • Rate Input:

    • Similar structure as principal input.
    • Prompt for interest rate and validate it.
  • Time Input:

    • Prompt user for time in years.
    • Typecast input to integer.
    • Validate input to ensure it's greater than zero.

Example Tests

  • Validations:
    • Negative principal or rate prompts error.
    • Valid values are accepted (e.g., $1000, 10%).

Interest Calculation

  • Formula for Compound Interest:
    • Total balance = Principal × (1 + (Rate / 100))^Time
  • Print new balance using formatted output.

Example Calculation

  • Scenario 1:

    • Principal: $1,000
    • Rate: 10%
    • Time: 1 year
    • New balance: $1,100
  • Scenario 2:

    • Principal: $500
    • Rate: 7%
    • Time: 2 years
    • New balance: $572.45

Allowing Zero Values

  • Variations for user input: Allow zero values by modifying the while loop conditions.
  • Use while true for loop with explicit break statements.

Example of Adjusted Code

  • Allow inputs of zero:
    • Example: Entering zero for principal leads to a displayed balance of zero.

Conclusion

  • Two methods to implement input validation:
    1. Standard while loop with conditions.
    2. While true with break statements.
  • The project demonstrates the use of while loops in Python with practical application in finance.