I am trying to find a savings rate (rate_approx
in my code) that will result in a savings (current_savings
in code) over 36 months that is within $100 of the down payment (down_payment
in code).
I'm running the for
loop each time within the while
loop to represent the growth of current_savings
over the 36 month period. Then once that savings rate is adjusted depending on whether it is too high or too low, I want it to save a new savings_rate
and try that on the 36 month period. So I reset current_savings
to make sure it's not starting from the current_savings
from the prior for
loop run and try again.
house_price = float(1000000)
down_payment = float(house_price*0.25)
high = float(1)
low = float(0)
rate_approx = float((low + high)/2)
salary = float(150000)
monthly_salary = float(salary/12)
monthly_contribution = float(monthly_salary*rate_approx)
semi_annual_raise = float(1.07)
APR = float(1.04)
current_savings = 0
while abs(current_savings - down_payment) > 100:
current_savings = 0
for n in range(35):
current_savings = float((current_savings + monthly_contribution) * APR)
if n > 0 and n%6 == 0:
monthly_salary = float(monthly_salary * semi_annual_raise)
monthly_contribution = float(monthly_salary*rate_approx)
if current_savings < down_payment + 100:
current_savings = 0
low = rate_approx
rate_approx = (low + high)/2
print("low")
print("savings rate:",rate_approx)
elif current_savings > down_payment + 100:
current_savings = 0
high = rate_approx
rate_approx = (low + high)/2
print("high")
print("savings rate:",rate_approx)
else:
break
print("savings rate:", float(rate_approx))
print("savings:", float(current_savings))
Why does the program continue outputting smaller and smaller savings rates that are giving larger and larger final current_savings
values?