简体   繁体   中英

How do I get two variables to update in a for loop in python for a compounding interest equation

I have a function written that calculates future value for a compounding interest (annual) given initial amount invested, rate of interest, and however many years it is compounded. I have it printing to a table, but the starting values are not updating per year correctly nor are the annual payments. I don't know how I can get them to update correctly in the for loop.

Here is the code:

print("Welcome to the Annual Investment Report Calculator! \n\nPlease enter the values below.\n")
initialAmt = float(input("- Enter your investment amount: "))
interest = float(input("- Enter the interest rate: "))
termPeriod = int(input("- Enter the time period, in years, for your investment: "))


#Function to calculate and print the values
def calculateValue(initialAmt, interest, termPeriod):
    for termPeriod in range(termPeriod):
        total = initialAmt * (((1 + ((interest/100.0))) ** (termPeriod+1)))
        total = float(round(total, 2))
        annualIncome = total - initialAmt
        annualIncome = float(round(annualIncome, 2))
        print('{:<15} {:<25} {:<25} {:<20}'.format((termPeriod+1), initialAmt, annualIncome, total))
    return total


def main():
    print("\n")
    title = "Annual Fixed Investment Income\n"
    yearT = "Year"
    strtBalT = "Starting Balance"
    annIncT = "Annual Income"
    endBalT = "Ending Balance"


#Formats title and headings
    print('{:^85}'.format(title))                                               
    print('{:<15} {:<25} {:<25} {:<20}'.format(yearT, strtBalT, annIncT, endBalT)) 
    calculateValue(initialAmt, interest, termPeriod)                                                    


main()
print("Welcome to the Annual Investment Report Calculator! \n\nPlease enter the values below.\n")
initialAmt = float(input("- Enter your investment amount: "))
interest = float(input("- Enter the interest rate: "))
termPeriod = int(input("- Enter the time period, in years, for your investment: "))


#Function to calculate and print the values
def calculateValue(initialAmt, interest, termPeriod):
    for termPeriod in range(termPeriod):
        total = 100 * (((1 + ((interest/100.0))) ** (termPeriod+1)))
        total = float(round(total, 2))
        annualIncome = total - initialAmt
        annualIncome = float(round(annualIncome, 2))
        print('{:<15} {:<25} {:<25} {:<20}'.format((termPeriod+1), initialAmt, annualIncome, total))
        initialAmt = total
    return total


def main():
    print("\n")
    title = "Annual Fixed Investment Income\n"
    yearT = "Year"
    strtBalT = "Starting Balance"
    annIncT = "Annual Income"
    endBalT = "Ending Balance"


#Formats title and headings
    print('{:^85}'.format(title))                                               
    print('{:<15} {:<25} {:<25} {:<20}'.format(yearT, strtBalT, annIncT, endBalT)) 
    calculateValue(initialAmt, interest, termPeriod)                                                    


main()

Here is the answer to the question the for loop just needed to be edited and reordered, but this works and gives correct values. Im sure it could be made simpler and less ugly if anyone wants to do it justice.

print("Welcome to the Annual Investment Report Calculator! \n\nPlease enter the values below.\n")
initialAmt = float(input("- Enter your investment amount: "))
interest = float(input("- Enter the interest rate: "))
termPeriod = int(input("- Enter the time period, in years, for your investment: "))


#Function to calculate and print the values
def calculateValue(initialAmt, interest, termPeriod):
    for termPeriod in range(termPeriod):
        total = initialAmt * ((1 + ((interest/100.0))))
        total = float(round(total, 2))
        annualIncome = total - initialAmt
        annualIncome = float(round(annualIncome, 2))
        print('{:<15} {:<25} {:<25} {:<20}'.format((termPeriod+1), initialAmt, annualIncome, total))
        initialAmt = total
    return total


def main():
    print("\n")
    title = "Annual Fixed Investment Income\n"
    yearT = "Year"
    strtBalT = "Starting Balance"
    annIncT = "Annual Income"
    endBalT = "Ending Balance"


#Formats title and headings
    print('{:^85}'.format(title))                                               
    print('{:<15} {:<25} {:<25} {:<20}'.format(yearT, strtBalT, annIncT, endBalT)) 
    calculateValue(initialAmt, interest, termPeriod)                                                    


main()

Here is the answer that works for all inputs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM