所以我试图解决这个问题;图像包含一些示例输出。你知道吗
这是我到目前为止的代码,我不知道我哪里出错了。问题是它没有在示例运行中显示正确的数字。你知道吗
investmentAmount=0
intr=0
monthlyInterestRate=0
def futureInvestmentValue(investmentAmount, monthlyInterestRate, years):
futureInvestmentValue=investmentAmount*(1+monthlyInterestRate)**years
return futureInvestmentValue
def main():
investmentAmount=int(input("The amount invested: "))
intr=int(input("Annual interest rate: "))
monthlyInterestRate=intr/1200
print("Years Future Value")
for yrs in range(1,31):
FIV=futureInvestmentValue(investmentAmount,monthlyInterestRate,yrs)
print(yrs, format(FIV, ".2f"))
main()
你年复一年的困惑。在您的代码中,您实际上是在以月为单位计算增量,尽管已经命名了变量
years
。你可能想把利率转换成float
而不是int
,这样可以有更大的范围。你知道吗下面是更正的代码(我没有更改公式):
正如您在输出中看到的,在60个月(5年)时,输出是您预期的:
相关问题 更多 >
编程相关推荐