Skip to content
Advertisement

Why doesn’t the break statement work in this program?

My program is supposed to tell users how many months it will take to double the money in their investment account. I am able to do the calculations correctly, but I’m unable to break out of the loop and print the statement that tells the user the final sentence “It will take x months to double your investment with a y% return”.

balance = int(input("Enter an initial Roth IRA deposit amount:"))
apr = int(input("Enter an annual percent rate of return:"))

month = 0

while balance != 2*balance:
    added_interest = balance * (apr / 100) / 12
    balance = balance + added_interest
    month +=1
    formatted_balance = "${:.2f}".format(balance)
    print("Value after month", month,":", formatted_balance)
    if balance == 2*balance:
        break
print("It will take", month, "months to double your investment with a", apr, "% return")

Advertisement

Answer

Your problem is that testing balance against 2*balance is always testing the current balance, not double the initial balance. Just store off the computed doubled balance initially, and test if the current balance is still less than that (no need for separate if/break, your while condition will handle it):

balance = int(input("Enter an initial Roth IRA deposit amount:"))
apr = int(input("Enter an annual percent rate of return:"))

month = 0

doubled_balance = 2 * balance  # Store off doubled initial balance

while balance < doubled_balance:  # Check current balance against doubled initial,
                                  # and use <, not !=, so you stop when you exceed it,
                                  # not just when you're exactly equal
    added_interest = balance * (apr / 100) / 12
    balance = balance + added_interest
    month +=1
    formatted_balance = "${:.2f}".format(balance)
    print("Value after month", month,":", formatted_balance)
    # No need for if/break
print("It will take", month, "months to double your investment with a", apr, "% return")

All that said, this doesn’t need a loop at all. The initial balance doesn’t matter (it takes just as long to double $1 as to double $1000 with a fixed rate of return, ignoring rounding errors), so this reduces to a simple conversion for APR to APY to account for monthly compounding, followed by a logarithm computation to figure out what power of the APY is necessary to reach 2 (a doubling), then convert from months to years and round up (since you won’t double until the end of that month):

import math

apr = int(input("Enter an annual percent rate of return:"))
apr_decimal = apr / 100
apy = (1 + (apr_decimal / 12)) ** 12 # Standard APR to APY computation for monthly compounding
months = math.ceil(math.log(2, apy) * 12)  # Compute the power (years) of APY to double the investment
                                           # then multiply by 12 and round up to a full month
print("It will take", months, "months to double your investment with a", apr, "% return")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement