Calculate Anual Rate Of Return In Python
Hey there! We’re going to work on a challenge together where we’ll be writing a program called invest.py. The goal of this program is to track the growing amount of an investment over time.
We’ll start with an initial deposit, also known as the principal amount. Each year, this amount will increase by a fixed percentage called the annual rate of return.
For instance, if we start with a principal amount of $100 and an annual rate of return of 5%, the first year’s increase will be $5. The second year, the increase will be 5% of the new amount, which is $105, resulting in an increase of $5.25.
To complete this challenge, we need to write a function called “invest” with three parameters: the principal amount, the annual rate of return, and the number of years to calculate.
The function should look something like this:
`def invest(amount, rate, years):`
Once we’ve defined the function, we need to ensure it prints out the amount of the investment, rounded to 2 decimal places, at the end of each year for the specified number of years.
For example, if we call the function like this:
invest(100, .05, 4), it should print out the following:
Doing with ‘for’ loop
Doing with ‘while’ loop