Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python进行复利_Python_Unit Testing - Fatal编程技术网

使用Python进行复利

使用Python进行复利,python,unit-testing,Python,Unit Testing,我正在写一个测试,按年度返回利息总额。在我的Investor类中的数组interest=[]内的每个步骤上合成时,如何附加感兴趣的值?出于某种原因,我很难理解这一点 代码: class Test(unittest.TestCase): '''Represents my test case(s) for the dojo.''' def setUp(self): '''Defines the initial state of an investor.''' self.inves

我正在写一个测试,按年度返回利息总额。在我的Investor类中的数组
interest=[]
内的每个步骤上合成时,如何附加感兴趣的值?出于某种原因,我很难理解这一点

代码:

class Test(unittest.TestCase):

'''Represents my test case(s) for the dojo.'''

def setUp(self):
    '''Defines the initial state of an investor.'''
    self.investor = Investor(10000, 7.2, 5)

def test_solve_for_interest(self):
    self.assertEqual(self.investor.get_interest(), 720.0)

def test_get_interest_by_year(self):
    '''Returns a value of interest associated by the year.'''
    self.assertEqual(self.investor.interest_by_year(1), 10720.0)


class Investor(object):

    def __init__(self, amount, rate, time):
        self.amount = amount
        self.rate = rate
        self.time = time

    def get_interest(self):
        return (self.amount * self.rate) / 100.0

    def interest_by_year(self, year):
        interest = []
        for i in xrange(1, (self.time+1)):
            # collect compounding interest each year
        return interest[year]
year 1 = 10,720.00 (interest[1])

year 2 = 11491.84  (interest[2])

year 3 = 12319.25  (interest[3])

year 4 = 13206.24  (interest[4])

year 5 = 14157.09  (interest[5])
预期结果:

class Test(unittest.TestCase):

'''Represents my test case(s) for the dojo.'''

def setUp(self):
    '''Defines the initial state of an investor.'''
    self.investor = Investor(10000, 7.2, 5)

def test_solve_for_interest(self):
    self.assertEqual(self.investor.get_interest(), 720.0)

def test_get_interest_by_year(self):
    '''Returns a value of interest associated by the year.'''
    self.assertEqual(self.investor.interest_by_year(1), 10720.0)


class Investor(object):

    def __init__(self, amount, rate, time):
        self.amount = amount
        self.rate = rate
        self.time = time

    def get_interest(self):
        return (self.amount * self.rate) / 100.0

    def interest_by_year(self, year):
        interest = []
        for i in xrange(1, (self.time+1)):
            # collect compounding interest each year
        return interest[year]
year 1 = 10,720.00 (interest[1])

year 2 = 11491.84  (interest[2])

year 3 = 12319.25  (interest[3])

year 4 = 13206.24  (interest[4])

year 5 = 14157.09  (interest[5])

测试:

输出:

year 1 = 10720.0
year 2 = 11491.84
year 3 = 12319.25248
year 4 = 13206.2386586
year 5 = 14157.087842    

用熊猫可以更优雅地解决这个问题:

years = range(1,6)
rate = 0.072
rates = pandas.Series(index = years, data = rate)
请注意,这些年的利率可能有所不同

initialValue*((rates + 1).cumprod())

嗯,我得到了一个
AssertionError:72000.0!=10720.0
这样我的代码计算第一年利息为72000.0?您输入了正确的利率吗?对于测试,我知道是self.investor.interest,by,year,self.investor.interest,by,year(1),10720.0,但是作为我的测试,
intest,by,year(1)
输出了10720。所以我想知道你把速率设置为1.072对吗?我用7.2初始化了它,所以我需要把它除以100,然后加上1.0。另外,你能解释一下这背后的逻辑吗?这本应该是一个快速热身的道场,但它让我在准备最后一部分时遇到了不少麻烦。