简体   繁体   中英

How can I make recursion function in Python?

I'm making a loan calculator that calculates what percentage of your salary(0.5 = 50 %) you have to save monthly in order to buy a house in 36 months. I wanted to start with 0.5 because it's in the middle,and if portion saved is too high to access high() and if the portion is not enough to access low() until it finds the right amount. In this example it keeps returning the same value( 0.5). How can I make it work?

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)

Maybe this answer your question.

    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))

Maybe it is better to have your while loop outside of your calculate function. I also changed your while loop condition and the low and high functions, so it converges quickly. You could get inspiration from this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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