Following is the problem set from MIT opencourseware
Part C: Finding the right amount to save away
- Your semiannual raise is .07 (7%)
- Your investments have an annual return of 0.04 (4%)
- The down payment is 0.25 (25%) of the cost of the house
- The cost of the house that you are saving for is $1M.
I am now going to try to find the best rate of savings to achieve a down payment on a $1M house in 36 months. And I want your savings to be within $100 of the required down payment.I am stuck with the bisection search and 'It is not possible to pay the down payment in three years.' this output. I am new to programmers and English.Any help is appreciated.
And here is my code:
starting_salary = float(input("Enter your starting salary: "))
months_salary = starting_salary/12
total_cost = 1000000.0
semi_annual_rate = .07
investment_return = 0.04
down_payment = total_cost * 0.25
r = 0.04
current_savings = 0.0
#months = 36
tolerance = 100
steps = 0
high = 1.0
low = 0.0
guess = (high+low)/2.0
total_salaries = 0.0
def calSavings(current_savings,months_salary,guess,month):
for months in range(0,37):
if months%6==1 and months >1:
months_salary=months_salary*(1+semi_annual_rate)
current_savings = current_savings + months_salary * guess
current_savings = current_savings * (1+0.04/12)
return(current_savings)
current_savings = calSavings(current_savings,months_salary,guess,1)
while abs(current_savings-down_payment)>=100:
if current_savings < down_payment:
low = guess
elif current_savings > down_payment:
high = guess
else:
print("It is not possible to pay the down payment in three years.")
break
guess = (low+high)/2
steps = steps +1
print("Best saving rate: ", guess)
When I run this code, it will be stuck. I don't know why. And I don't know how to determine which is the condition of output this "It is not possible to pay the down payment in three years."
I have seen Similar questions on stackoverflow like this and this but I am not quite followed.
current_savingsnordown_paymentare being updatedcurrent_savings = calSavings(current_savings,months_salary,guess,1)inside the while loop. Also to break add these lines:if (steps > 1000): print("It is not possible to pay the down payment in three years.") breakinstead ofelse: print("It is not possible to pay the down payment in three years.") break