繁体   English   中英

我如何在python的for循环中更新两个变量以获得复利方程

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

我编写了一个函数,可以计算给定初始投资金额、利率以及复利多年的复利(年度)的未来值。 我将它打印到表格上,但起始值没有正确每年更新,年付款也没有正确更新。 我不知道如何让它们在 for 循环中正确更新。

这是代码:

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()

以下是问题的答案 for 循环只需要编辑和重新排序,但这有效并提供正确的值。 我敢肯定,如果有人想要公正地对待它,它可以变得更简单、更不丑陋。

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()

这是适用于所有输入的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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