Python Programming – John Zelle / Chapter 2 Exercise 6

Suppose you have an investment plan where you invest a certain fixed amount every year. Modify futval.py to compute the total accumulation of your investment. The inputs to the program will be the amount to invest every year, the interest rate, and the number of years for the investment.

Chapter 2 Exercise 6

Python

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #Chapter 2 Exercise 6 def main(): print("This program calculates the future value of an investment plus yearly contributions.") principal = eval(input("Enter the initial principal: ")) eachyr = eval(input("How much do you want to invest every year?: ")) apr = eval(input("Enter the annual interest rate: ")) yr = eval(input("Enter the number of years: ")) for i in range (yr): principal = principal * (1 + apr) + eachyr print("The value in", yr , "years is $""{0:.2f}" .format(principal)) main() |