代码之家  ›  专栏  ›  技术社区  ›  Nicole Foster

可变利率。当利率符合条件时,我怎样才能让它转换呢

  •  -1
  • Nicole Foster  · 技术社区  · 6 年前

    我正试图让利率在总复合利率达到其范围时自动转换。 我输入,校长,每月的总数和学期。 它只计算第一年的复利。我希望它能够将这个数量发送回函数,以便它重新计算,直到术语结束。

    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
    

    所以我用网络学习(10000,10000),得到10125.06,这是第一年的成绩。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Scratch'N'Purr    6 年前
    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)
    
        2
  •  0
  •   Stephen Rauch Afsar Ali    6 年前

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

    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