在Python中寻找未来的投资价值
我是一名初级程序员,对根据以下公式计算未来投资价值有疑问: 未来投资价值=投资金额*(1+月利率)月数 ... ofc的numberOfMonths值是一个指数。在Python中寻找未来的投资价值,python,Python,我是一名初级程序员,对根据以下公式计算未来投资价值有疑问: 未来投资价值=投资金额*(1+月利率)月数 ... ofc的numberOfMonths值是一个指数。 到目前为止,我已经创建了这个,但在运行程序时似乎收到了错误的答案 #Question 2 investmentAmount = float(input("Enter investment amount: ")) annualInterestRate = float(input("Enter annual interest rate
到目前为止,我已经创建了这个,但在运行程序时似乎收到了错误的答案
#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)
我需要解决什么问题才能使这件事正常运行,如果您能提供任何帮助,我们将不胜感激。谢谢您。
年度利率战略
应该除以12才能获得月度利率战略
正确的最终公式应该是
futureInvestmentValue = investmentAmount * (1 + (monthlyInterestRate/100) ** \
numberOfMonths
您可以这样做:
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)
错误在于:
monthlyInterestRate = ((annualInterestRate)/10)/12
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
numberOfMonths
我认为有两个错误。第一个是利率被10除,而利率应该被100除。现在,如果您输入2,它将被视为20%的利息,因为2/10
=.2
第二个错误是,MonthlyInterestate
假设一个固定利率,而futureInvestmentValue
假设一个复合利率。应该是
月利率=(1+(年利率/100))**(.1/1.2)
例如(使用/12):
输出:
0.00416666666667
1.05116189788
复利的月利率不等于一年的年利率。这是因为在一种情况下你除以12,下一种情况下你提升到12的幂,这是不相等的
示例(使用**1/12)
您只有一个错误,即“eval”“似乎收到了错误的答案”?也许你确定的时候应该回来,解释一下问题是什么。我真不敢相信我错过了这么一个简单的错误,非常感谢。正如我所说,我是一个初学者程序员,还不熟悉调试代码的过程,不管是逻辑错误还是语法错误。没问题!如果你已经解决了这个问题,请把它标记为已回答!如果得到意外的结果,应该打印中间步骤,通常这将有助于找到错误
0.00416666666667
1.05116189788
from __future__ import division
print (1.05**(1/12))**12 #returns 1.05
investmentAmount = eval(input("Enter investment amount: "))
annualInterestRate = eval(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)