Python Challenge: Compound Interest Calculator

Python Mini Challenge with Solutions
Compound Interest Calculator
Description
In this Python mini challenge with solutions you will have to build a compound interest calculator. Your task is to create a Python program that calculates the future value of an investment based on the principal amount, interest rate, and compounding period. It is a useful tool for individuals who want to estimate the growth of their investments over time.
The program will work as follows: First it will ask the user to enter the data: Principal amount, the interest rate and the time period. Then it will apply the compund interest formula (see below) and display the result.
Expected output
Enter the principal amount: 1000
Enter the interest rate (in decimal form): 0.05
Enter the number of years: 5
Future Value: $1276.28
Steps
1) Create a new Python file
- Open your code editor or IDE and create a new Python file (e.g., compound_interest_calculator.py).
2) Define the main function
- Start by defining the main function that will contain the logic of the compound interest calculation.
3) Gather user input
- Prompt the user to enter the principal amount, interest rate (in decimal form), and the number of years for the investment.
- Use the
"input()"function to capture the user’s input and store them in appropriate variables.
4) Calculate the compound interest
- Use the compound interest formula to calculate the future value of the investment yearly:
Future Value = Principal Amount * (1 + Interest Rate/Compounding Period)^(Compounding Period * Number of Years)
Store the result in a variable.
5) Display the result
- Print the calculated future value of the investment to the console.
- Format the output to make it more user-friendly.
6) Test the program
- Run the Python script and test the compound interest calculator by entering different values for the principal amount, interest rate, and number of years.
Python challenge solutions
def compound_interest(principal, rate, time):
amount = principal * (1 + rate / 100) ** time
return amount
def main():
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (in percentage): "))
time = float(input("Enter the time period (in years): "))
amount = compound_interest(principal, rate, time)
print(f"The compound interest amount is: {amount:.2f}")
if __name__ == "__main__":
main()
