I am new to python, I will really appreciate it if someone can help me find the errors in my code. Thank you in advance.
The problem asks us to find the best rate of savings to achieve a down payment on a house in 36 months. Since hitting this exactly is a challenge, the answer only needs to be within $100 of the required down payment.
To simplify things, assume:
- 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.
Here is my original code:
annual_salary=float(input("Enter your annual salary:"))
semi_annual_raise=0.07
total_cost=1000000
portion_down_payment = 0.25
current_saving=0
r=0.04
low=0
high=10000
guess=(low high)/2
numGuesses=0
while abs(portion_down_payment*total_cost-current_saving)>=100:
current_saving=0
portion_saved=guess/10000
for i in range (36):
current_saving =current_saving*r/12 annual_salary/12*portion_saved
if i%6==0 and i>0:
annual_salary =annual_salary*semi_annual_raise
if portion_down_payment*total_cost-current_saving>0:
low=guess
else:
high=guess
guess=(low high)/2
numGuesses =1
if numGuesses > 1000:
break
if numGuesses>1000:
print("It is not possible to pay the down payment in three years.")
else:
rate=guess/10000
print("steps in bisection search is",numGuesses,"and the best saving rate is",rate)
When 150000 was entered as annual_salary, the result is:
It is not possible to pay the down payment in three years.
Because it wouldn’t return the correct answer, I used a function to calculate the curring saving and the code is shown below:
annual_salary=float(input("Enter your annual salary:"))
semi_annual_raise=0.07
total_cost=1000000
portion_down_payment = 0.25
r=0.04
low=0
high=10000
guess=(low high)/2
numGuesses=0
def current_saving(annual_salary,guess,months):
portion_saved=guess/10000
saving=0
r=0.04
semi_annual_raise=0.07
for i in range (months):
saving =saving*r/12 annual_salary/12*portion_saved
if i%6==0 and i>0:
annual_salary =annual_salary*semi_annual_raise
return(saving)
total=0
while abs(portion_down_payment*total_cost-total)>=100:
total=current_saving(annual_salary,guess,36)
if portion_down_payment*total_cost-total>0:
low=guess
else:
high=guess
guess=(low high)/2
numGuesses =1
if numGuesses > 1000:
break
if numGuesses>1000:
print("It is not possible to pay the down payment in three years.")
else:
rate=guess/10000
print("steps in bisection search is",numGuesses,"and the best saving rate is",rate)
When 150000 was entered as annual_salary this time, the result is:
steps in bisection search is 7 and the best saving rate is 0.44140625
I feel the two versions basically do the same thing, how come the first one wouldn't work but the second do? I assume there is something wrong within the while loop, but I could not pinpoint my error. Thank you so much again for any help!
CodePudding user response:
In the version that does not work, you do not re-set annual_salary
on each iteration of the while loop.
In the version that does work, annual_salary
the local variable is reset each time your method is called.
to make the broken one work:
## save the original value
annual_salary_original = annual_salary
while abs(portion_down_payment * total_cost - current_saving) >= 100:
## reset salary on each iteration
annual_salary = annual_salary_original
...