【问题标题】:Variable interest rate. How can I get the rate to switch when interest it fits the condition可变利率。当利息符合条件时,我怎样才能让利率切换
【发布时间】:2019-02-28 16:51:09
【问题描述】:

我试图让利率在总复合达到其范围内时自动切换。 我输入,校长,每月总数和期限。 它只计算第一年的复利。我希望它能够将该金额发送回函数中,以便它重新计算,直到学期结束。

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,这对于第一年来说是正确的。

【问题讨论】:

  • 它在我的工作。只是它只给了我第一个总数。
  • 我假设invested 真的是你的术语?
  • 是的。你是对的。

标签: python python-3.x loops recursion


【解决方案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)

【讨论】:

  • 你如何总结总利息?
  • @NicoleFoster 编辑了同样返回应计利息的帖子 :)
【解决方案2】:

您可以通过以下搜索将您的范围转换为利率:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多