浮动利率。当利率符合条件时,如何获得利率转换

时间:2018-09-25 06:23:39

标签: python python-3.x loops recursion

我试图使利率在总化合物达到其范围时自动切换。 我输入本金,每月总计和学期。 它仅计算第一年的复利。我希望它能够将该金额发送回函数中,以便它可以再次进行重新计算,直到学期结束为止。

def IntEarned(p, m, t):
    principal = p
    monthly_deposit = m
    invested = t


        currentamount = principal + (monthly_deposit * 12)

    def currentTest():

        if currentamount < 100000:
            interest =  (0.05 / 100)
            return interest
        elif currentamount < 200000:
            interest =  (0.10 / 100)
            return interest
        elif currentamount < 250000:
            interest =  (0.15 / 100)
            return interest
        elif currentamount < 500000:
            interest = (0.25 / 100)
            return interest
        elif currentamount < 1000000:
            interest = (0.40 / 100)
            return interest
        elif currentamount < 2000000:
            interest =  (0.55 / 100)
            return interest
        elif currentamount < 5000000:
            interest =  (0.60 / 100)
            return interest
        elif currentamount >= 5000000:
            interest =  (0.70 / 100)
            return interest



    while (invested > 0):
        currentamount = currentamount + (currentamount * currentTest())
        invested = invested - 1
        return currentamount

所以我用IntEarned(10000,10,1000))运行它,我得到了10125.06,这是第一年的正确记录。

2 个答案:

答案 0 :(得分:1)

def calc_accrual(p, m, t, i):
    t -= 1
    p = p + (m * 12)

    if p < 100000:
        i += p * 0.0005
        p *= 1.0005
    elif p < 200000:
        i += p * 0.0010
        p *= 1.0010
    elif p < 250000:
        i += p * 0.0015
        p *= 1.0015
    elif p < 500000:
        i += p * 0.0025
        p *= 1.0025
    elif p < 1000000:
        i += p * 0.0040
        p *= 1.0040
    elif p < 2000000:
        i += p * 0.0055
        p *= 1.0055
    elif p < 5000000:
        i += p * 0.0060
        p *= 1.0060
    else:
        i += p * 0.0070
        p *= 1.0070

    if t > 0:
        return calc_accrual(p, m, t, i)
    else:
        return p, i

测试

>>> calc_accrual(10000, 10, 1, 0)
>>> (10125.06, 5.0600000000000005)
>>> calc_accrual(10000, 10, 1000, 0)
>>> (204357.25738374083, 74357.25738374837)

答案 1 :(得分:0)

您可以使用以下搜索将范围转换为利率:

def interest_per_value(value):
    interest_table = (
        (100000, 0.05),
        (200000, 0.10),
        (250000, 0.15),
        (500000, 0.25),
        (1000000, 0.40),
        (2000000, 0.55),
        (5000000, 0.60),
    )
    for test_value, rate in interest_table:
        if value < test_value:
            return rate / 100
        return 0.70 / 100