[英]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.