寻找Python的未来投资价值

2024-09-26 22:50:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个初级程序员,有一个关于根据以下公式计算未来投资价值的问题: futureInvestmentValue=投资金额*(1+月利率)月数 ... ofc numberOfMonths值是一个指数。 到目前为止,我已经创建了这个程序,但是在运行程序时似乎收到了不正确的答案

#Question 2

investmentAmount = float(input("Enter investment amount: "))

annualInterestRate = float(input("Enter annual interest rate: "))

monthlyInterestRate = ((annualInterestRate)/10)/12

numberOfYears = eval(input("Enter number of years: "))

numberOfMonths = numberOfYears * 12

futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
                     numberOfMonths
print("Accumulated value is", futureInvestmentValue)

我需要做些什么才能使这件事正常工作,任何帮助都将不胜感激谢谢


Tags: 程序inputfloat金额程序员公式价值enter
3条回答

错误在:

monthlyInterestRate = ((annualInterestRate)/10)/12
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
                     numberOfMonths

我认为有两个错误。第一个是利率被10除,而它应该被除以100。现在,如果输入2,它将被视为20%的利息,因为2/10=.2。在

第二个错误是monthlyInterestRate假设一个固定利率,而{}假设复合利率。应该是的 monthlyInterestRate = (1 + (annualInterestRate/100))**(.1/1.2)。在

例如(使用/12):

^{pr2}$

输出:

0.00416666666667
1.05116189788

月利率复利不等于一年的年利率。这是因为在一个例子中你除以12,下一个例子,你提高到12的幂,这是不相等的。在

示例(使用**1/12)

from __future__ import division
print (1.05**(1/12))**12 #returns 1.05

你可以这样做:

from __future__ import division # assuming python 2.7.xxxxx

investmentAmount = float(input("Enter investment amount: "))

annualInterestRate = float(input("Enter annual interest rate: "))

monthlyInterestRate = ((annualInterestRate)/10)/12

try:
   numberOfYears = int(input("Enter number of years: "))
except Exception, R:
   print "Year must be a number"

numberOfMonths = numberOfYears * 12

futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
                 numberOfMonths
print("Accumulated value is", futureInvestmentValue)

annualInterestRate应除以12得到monthlyInterestRate。在

正确的最终公式应该是

futureInvestmentValue = investmentAmount * (1 + (monthlyInterestRate/100) ** \
                        numberOfMonths

相关问题 更多 >

    热门问题