繁体   English   中英

如何在 Python 中递归 function?

[英]How can I make recursion function in Python?

我正在制作一个贷款计算器,计算你必须每月储蓄多少百分比(0.5 = 50%)才能在 36 个月内买房子。 我想从 0.5 开始,因为它在中间,如果保存的部分太高而无法访问 high() 并且该部分不足以访问 low() 直到找到合适的数量。 在此示例中,它一直返回相同的值 (0.5)。 我怎样才能让它发挥作用?

def calculate(portion_saved):

    anual_salary = 300000
    semi_annual_raise = 0.07
    total_cost = 1000000
    portion_down_payment = 0.25 * total_cost
    nr_months = 0
    current_savings = 0

    for x in range(1, 37):
        monthly_salary = anual_salary / 12
        monthly_interest = 0.04 / 12 * current_savings
        current_savings = current_savings + monthly_salary*portion_saved + (current_savings*0.04)/12
        nr_months += 1
        if nr_months % 6 == 0:
            anual_salary = anual_salary + (anual_salary*semi_annual_raise)


    while current_savings + 100.00 != portion_down_payment or current_savings - 100.00 != portion_down_payment:
        if current_savings + 100.00 > portion_down_payment or current_savings - 100.00 > portion_down_payment:
            high(portion_saved)

        else:
            low(portion_saved)

    print(current_savings, portion_down_payment)


def high(portion_saved):
    return portion_saved / 2


def low(portion_saved):

    return portion_saved + (1 - portion_saved) / 2


calculate(0.5)

也许这回答了你的问题。

    while current_savings + 100.00 != portion_down_payment or current_savings - 100.00 != portion_down_payment:
        if current_savings + 100.00 > portion_down_payment or current_savings - 100.00 > portion_down_payment:
            calculate(high(portion_saved))

        else:
            calculate(low(portion_saved))

也许最好将while loop放在calculate function 之外。我还更改了while loop condition以及lowhigh函数,因此收敛很快。 你可以从中得到灵感:

def calculate(portion_saved):

    anual_salary = 300000
    semi_annual_raise = 0.07
    total_cost = 1000000
    portion_down_payment = 0.25 * total_cost
    nr_months = 0
    current_savings = 0

    for x in range(1, 37):
        monthly_salary = anual_salary / 12
        monthly_interest = 0.04 / 12 * current_savings
        current_savings = current_savings + monthly_salary*portion_saved + (current_savings*0.04)/12
        nr_months += 1
        if nr_months % 6 == 0:
            anual_salary = anual_salary + (anual_salary*semi_annual_raise)

    print("portion_saved", portion_saved)
    print("current_savings", current_savings)
    print("portion_down_payment", portion_down_payment)
    print("")
    return current_savings

def high(portion_saved):
    # return portion_saved / 2
    return portion_saved / 1.5

def low(portion_saved):
    return portion_saved * 1.3
    # return portion_saved + (1 - portion_saved) / 2

portion_saved = 0.5
current_savings = calculate(portion_saved)

total_cost = 1000000
portion_down_payment = 0.25 * total_cost

while current_savings > portion_down_payment + 1000.00 or current_savings < portion_down_payment - 1000.00:
    if current_savings > portion_down_payment + 1000.00:
        portion_saved = high(portion_saved)
    else:
        portion_saved = low(portion_saved)
    current_savings = calculate(portion_saved)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM