0

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Gwen Di
  • 1
  • 2
  • Now would be a good time to learn the debugging features that Python or your IDE supports. But what does the code currently do or output vs what you expect? – OneCricketeer Jan 04 '22 at 03:43
  • By the way, if you generated a list of all 36 elements, you can use the bisect module https://docs.python.org/3/library/bisect.html – OneCricketeer Jan 04 '22 at 03:46
  • 1
    Totally! Excuse me if this isn't what IDE means, but I'm using Spyder. Currently, the code is consistently printing "high" and giving me a lower and lower savings rate, but the current_savings is actually getting higher and higher. I suspect the current_savings isn't resetting, but that doesn't make sense to me since I reset it literally right after the while loop. – Gwen Di Jan 04 '22 at 03:48
  • Spyder is an IDE, yes. See https://docs.spyder-ide.org/5/panes/debugging.html And, is the indentation of the code you posted correct? Your if statements are outside of the loop, so the break statement for example doesn't do anything – OneCricketeer Jan 04 '22 at 03:49
  • Nope. Those indentations were off. Fixed now. And thanks for this resource! For some reason, I suspect the current_savings formula right after the for loop is off, because on the second iteration of the while loop I'm getting a current_savings at n=0 of 9116.586249550004 but the rate_approx at that point is 0.25 which is less than the original rate_approx (=0.5) which gave a current_savings at n=0 equal to 6,500. – Gwen Di Jan 04 '22 at 03:57
  • Can you please edit the post to explain the algorithm logic? For example, why are you resetting the savings to zero in the loop? And I don't understand why the for loop needs to run each time with the while loop, and range(35) isn't counting up to 36 – OneCricketeer Jan 04 '22 at 13:24
  • @OneCricketeer Sure! Tried my best in the original post to explain my original logic. I will also try and attach a photo of how I mapped it out on paper. Thanks for pointing out that range(35) doesn't count up to 36. And thanks again for your help. – Gwen Di Jan 05 '22 at 00:40
  • @mkrieger1 My question is rather crude... I just don't understand why the program continues outputting smaller and smaller savings rates that are giving larger and larger final current_savings values. – Gwen Di Jan 05 '22 at 00:41
  • The indentation is still not quite right, by the way. – mkrieger1 Jan 05 '22 at 00:43
  • Hm... I want the program to run for the conditions of the while loop, first doing the iteration of n=1,...,36, then using the current_savings value from there as conditions for the if, elif, else statements. So wouldn't the for, if, elif, and else statements all be nested at the same level? – Gwen Di Jan 05 '22 at 00:50
  • So, did you debug yet, and found where the values are getting smaller/larger where you don't expect? To address comment, those would be indented at the same level, but they also need to be on the same level as `for` to be "part of the while loop". Also another thing to consider would be `(current_savings - down_payment) > 100` (or vice versa) for your conditions; the order of evaluation is important, and the subtraction makes the difference clearer (absolute value wouldn't hurt either). I.e. Either its within 100 dollars, or more. – OneCricketeer Jan 05 '22 at 01:57

0 Answers0