def futureValue(currentValue,apr,years): '''Given the current value of an investment (int or float), an APR (annual percentage rate) (a float), and a number of years (an int), return the future value of the investment after years years (rounded to an int), assuming simple interest. ''' # e.g. apr = .06 value = 1000 # # after year 1: 1000 + 1000 * .06 = 1060 # after year 2: 1060 + 1060 * .06 = 1123.60 # after year 3: 1123.60 + 1123.60 * .06 = 1191.016 # # note: 1000 + 1000 * .06 = 1000 * (1 + .06) # we'll use this simpler form newValue = currentValue for i in range(0,years): newValue = newValue * (1 + apr) return round(newValue) def grade(myAns,correctAnswer): # You can call this from a print-and-test function for any function if myAns == correctAnswer: print("Correct, the answer is",myAns) else: print("My answer is",myAns,"but the correct answer is",correctAnswer) def printTestFutureValue(currentValue,apr,years,correctAnswer): myAns = futureValue(currentValue,apr,years) print("The future value of a", currentValue, "investment with APR", apr, "after",years,"years is",myAns) grade(myAns,correctAnswer) def main(): #We could make the output look better by calling the "format" #function (exercise!) # printTestFutureValue(1000,.06,3,1191) print ("") printTestFutureValue(1000,.06,0,1000) print ("") printTestFutureValue(1000,0,3,1000) print ("") printTestFutureValue(0,.06,3,0) cd1val = futureValue(1000,.06,3) cd2val = futureValue(1500,.03,3) total = cd1val + cd2val #Suze Orman example - 45 year old woman with $74K in retirement to buy a $6400 pool? #Denied! If she invests this now at APR 4% and retires at 65, #she would have this much #more money print ("") print(futureValue(6400,.04,20)) main()