I have a program (futval.py) that will calculate the value of an investment after 10 years. I want to modify the program so that instead of calculating the value of a one time investment after 10 years, it will calculate the value of an annual investment after 10 years. I want to do this without using an accumulator variable. Is it possible to do this with only the variables that were present in the original program (investment, apr, i)?
# futval.py
# A program to compute the value of an investment
# carried 10 years into the future
def main():
print "This program calculates the future value",
print "of a 10-year investment."
investment = input("Enter the initial investment: ")
apr = input("Enter the annual interest rate: ")
for i in range(10):
investment = investment * (1 + apr)
print "The value in 10 years is:", investment
main()
I was not able to accomplish modifying the program without introducing the 'futval' accumulator variable.
# futval10.py
# A program to compute the value of an annual investment
# carried 10 years into the future
def main():
print "This program calculates the future value",
print "of a 10-year annual investment."
investment = input("Enter the annual investment: ")
apr = input("Enter the annual interest rate: ")
futval = 0
for i in range(10):
futval = (futval + investment) * (1+apr)
print "The value in 10 years is:", futval
main()