In: Computer Science
You are a personal financial advisor and you frequently need to
tell individuals how many years it will take them to reach their
retirement goals given their returns on their various accounts. You
plan to write a Python program that helps you calculate these
returns.
You must take in three pieces of input:
Given those inputs above, we need to write a loop (we don't know how many iterations) that will continue to add our interest to our balance, until we've reach our goal, at which point the looping stops. We want to know how many years (iterations) it took to reach this goal, so keep track of a count each iteration of the loop.
To add interest to a balance you can think of a balance variable called b and we would use the following math:
b = b + b * apr
If the APR is a deciaml 0.04, then 100 * 0.04 becomes 4... thus our first year earned us 4 dollars making our balancing 104.
Print out the balance and the year each iteration of the loop. See the example screenshot below:
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
b = float(input("How much money you wish to start a savings/market account with: ")) #Reading the starting balance
apr = float(input("Enter Annual Percentage Return as decimal: ")) #Reading the APR
goal_amt = float(input("Enter the goal amount: ")) #Reading the goal amount
print("\nYear MoneyEarned Balance") #printing table header
year = 1 #Initialize year as 1
while b < goal_amt: #Loop untill Balance is less than goal amount
earn = b * apr;
b = b + earn #calculate the balance after each year
print("{:>2d} {:>8.2f} {:>8.2f}".format(year, earn, b)) #displaying amount earned and balance for each year
year = year + 1 #increment year by 1
OUTPUT: