在Python中寻找未来的投资价值

时间:2016-06-19 22:04:20

标签: python

我是初学者程序员,根据以下公式对未来投资价值的计算有疑问: futureInvestmentValue = investmentAmount *(1 + monthlyInterestRate)numberOfMonths ... 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)

我需要修理什么才能使这个东西工作,任何帮助将不胜感激

4 个答案:

答案 0 :(得分:0)

annualInterestRate应除以12以获得monthlyInterestRate

正确的最终公式应为

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

答案 1 :(得分:0)

你可以这样做:

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)

答案 2 :(得分:0)

错误在:

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

我认为有两个错误。第一个是当利率除以100时,利率除以10.现在,如果输入2,则将其视为20%利息,因为2/10 = .2

第二个错误是monthlyInterestRate假定利率持平,而futureInvestmentValue则假定复利率。它应该是 monthlyInterestRate = (1 + (annualInterestRate/100))**(.1/1.2)

例如(使用/ 12):

print 0.05/12
print (1+0.05/12)**12

输出:

0.00416666666667
1.05116189788

月度利率复合不等于一年的年利率。因为在一种情况下你除以12,下一种情况你提升到12的幂是不相等的。

示例(使用** 1/12)

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

答案 3 :(得分:0)

private void TriggerDataGet()
{
ThreadPool.QueueUserWorkItem(o => {
    var myData = new myData();

    myData.Run(() =>
    {
        ShowResults(myData);
    },
    () => 
    {
        ShowResults(myData);
    });
});
 }

private void ShowResults( myData myDataResults )
{
this.Activity.RunOnUiThread(() =>
     {
        // use myDataResults save it to file.

    // make changes to the ui.


             // go to the results fragment.
             ResultsFragment resultsFragment = new ResultsFragment();
             resultsFragment.AddResults(inspector.Results);

             FragmentTransaction transaction = FragmentManager.BeginTransaction();

             transaction.Replace(Resource.Id.fragment_container, resultsFragment);
             transaction.AddToBackStack("res_frag");

             transaction.Commit();
});
}

你只有一个错误是“eval”