MIT programming in python Problem Set 1 Part C











up vote
-4
down vote

favorite
1












Following is the problem set from MIT opencourseware




Part C: Finding the right amount to save away




  1. Your semi­annual raise is .07 (7%)

  2. Your investments have an annual return of 0.04 (4%)

  3. The down payment is 0.25 (25%) of the cost of the house

  4. 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.










share|improve this question









New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Although you did put what you have done. You did not mention what exactly you are having problem with. Try to break the problem into parts and see if you can solve it that way.
    – MEdwin
    Nov 19 at 12:15










  • how do you break out of the while loop ? Since neither current_savings nor down_payment are being updated
    – Hadi Farah
    Nov 19 at 12:15












  • As I already said, your values are not updating. try adding this line: current_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.") break instead of else: print("It is not possible to pay the down payment in three years.") break
    – Hadi Farah
    Nov 19 at 13:09










  • basically when running this sort of recursive calculation you need 2 parameters for your while loop: one that indicates a maximum amount of steps and one for when you reach your goal to a certain tolerance, so a tolerance value.
    – Hadi Farah
    Nov 19 at 13:17










  • thank you , I will try to fix it
    – Adil
    Nov 19 at 13:29















up vote
-4
down vote

favorite
1












Following is the problem set from MIT opencourseware




Part C: Finding the right amount to save away




  1. Your semi­annual raise is .07 (7%)

  2. Your investments have an annual return of 0.04 (4%)

  3. The down payment is 0.25 (25%) of the cost of the house

  4. 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.










share|improve this question









New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Although you did put what you have done. You did not mention what exactly you are having problem with. Try to break the problem into parts and see if you can solve it that way.
    – MEdwin
    Nov 19 at 12:15










  • how do you break out of the while loop ? Since neither current_savings nor down_payment are being updated
    – Hadi Farah
    Nov 19 at 12:15












  • As I already said, your values are not updating. try adding this line: current_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.") break instead of else: print("It is not possible to pay the down payment in three years.") break
    – Hadi Farah
    Nov 19 at 13:09










  • basically when running this sort of recursive calculation you need 2 parameters for your while loop: one that indicates a maximum amount of steps and one for when you reach your goal to a certain tolerance, so a tolerance value.
    – Hadi Farah
    Nov 19 at 13:17










  • thank you , I will try to fix it
    – Adil
    Nov 19 at 13:29













up vote
-4
down vote

favorite
1









up vote
-4
down vote

favorite
1






1





Following is the problem set from MIT opencourseware




Part C: Finding the right amount to save away




  1. Your semi­annual raise is .07 (7%)

  2. Your investments have an annual return of 0.04 (4%)

  3. The down payment is 0.25 (25%) of the cost of the house

  4. 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.










share|improve this question









New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Following is the problem set from MIT opencourseware




Part C: Finding the right amount to save away




  1. Your semi­annual raise is .07 (7%)

  2. Your investments have an annual return of 0.04 (4%)

  3. The down payment is 0.25 (25%) of the cost of the house

  4. 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.







python python-3.x






share|improve this question









New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 19 at 13:00





















New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 12:02









Adil

32




32




New contributor




Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Adil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Although you did put what you have done. You did not mention what exactly you are having problem with. Try to break the problem into parts and see if you can solve it that way.
    – MEdwin
    Nov 19 at 12:15










  • how do you break out of the while loop ? Since neither current_savings nor down_payment are being updated
    – Hadi Farah
    Nov 19 at 12:15












  • As I already said, your values are not updating. try adding this line: current_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.") break instead of else: print("It is not possible to pay the down payment in three years.") break
    – Hadi Farah
    Nov 19 at 13:09










  • basically when running this sort of recursive calculation you need 2 parameters for your while loop: one that indicates a maximum amount of steps and one for when you reach your goal to a certain tolerance, so a tolerance value.
    – Hadi Farah
    Nov 19 at 13:17










  • thank you , I will try to fix it
    – Adil
    Nov 19 at 13:29


















  • Although you did put what you have done. You did not mention what exactly you are having problem with. Try to break the problem into parts and see if you can solve it that way.
    – MEdwin
    Nov 19 at 12:15










  • how do you break out of the while loop ? Since neither current_savings nor down_payment are being updated
    – Hadi Farah
    Nov 19 at 12:15












  • As I already said, your values are not updating. try adding this line: current_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.") break instead of else: print("It is not possible to pay the down payment in three years.") break
    – Hadi Farah
    Nov 19 at 13:09










  • basically when running this sort of recursive calculation you need 2 parameters for your while loop: one that indicates a maximum amount of steps and one for when you reach your goal to a certain tolerance, so a tolerance value.
    – Hadi Farah
    Nov 19 at 13:17










  • thank you , I will try to fix it
    – Adil
    Nov 19 at 13:29
















Although you did put what you have done. You did not mention what exactly you are having problem with. Try to break the problem into parts and see if you can solve it that way.
– MEdwin
Nov 19 at 12:15




Although you did put what you have done. You did not mention what exactly you are having problem with. Try to break the problem into parts and see if you can solve it that way.
– MEdwin
Nov 19 at 12:15












how do you break out of the while loop ? Since neither current_savings nor down_payment are being updated
– Hadi Farah
Nov 19 at 12:15






how do you break out of the while loop ? Since neither current_savings nor down_payment are being updated
– Hadi Farah
Nov 19 at 12:15














As I already said, your values are not updating. try adding this line: current_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.") break instead of else: print("It is not possible to pay the down payment in three years.") break
– Hadi Farah
Nov 19 at 13:09




As I already said, your values are not updating. try adding this line: current_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.") break instead of else: print("It is not possible to pay the down payment in three years.") break
– Hadi Farah
Nov 19 at 13:09












basically when running this sort of recursive calculation you need 2 parameters for your while loop: one that indicates a maximum amount of steps and one for when you reach your goal to a certain tolerance, so a tolerance value.
– Hadi Farah
Nov 19 at 13:17




basically when running this sort of recursive calculation you need 2 parameters for your while loop: one that indicates a maximum amount of steps and one for when you reach your goal to a certain tolerance, so a tolerance value.
– Hadi Farah
Nov 19 at 13:17












thank you , I will try to fix it
– Adil
Nov 19 at 13:29




thank you , I will try to fix it
– Adil
Nov 19 at 13:29












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I've already addressed most of my points in the comments, but I will recap:
You are trying to solve a problem using a method called recursive solving, in this case using the bisection method.



The steps are as follows:




  1. start with an initial guess you chose 0.5

  2. Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:

  3. Maximum number of steps before failure, remember we can always add 2 values and divide by 2, your result will tend to 0.999999.. otherwise

  4. A certain tolerance, if your step size is not small enough 25% of 1 M is 250 000 and you might never hit that number exactly, that' why you make a tolerance interval, for example: anything between 250 000 and 251 000 --> break loop, show result.

  5. Your if statements for changing low and high to adjust guess are correct, but you forget to re-initialize savings to 0 which means savings was going to infinity.


Now that's all said, here's a working version of your code:



starting_salary = 100000  # Assuming Annual Salary of 100k 
months_salary = starting_salary/12
total_cost = 1000000.0
semi_annual_rate = .07
investment_return = 0.04
down_payment = total_cost * 0.25
print("down payment: ", down_payment)
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
tolerance = down_payment/100 # I chose this tolerance to say if my savings are between [down_payment - (downpayment + down_payment/100)] result is admissible. (this is quite a high tolerance but you can change at leisure)
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)
return(current_savings)

while abs(current_savings-down_payment)>=100:
current_savings = calSavings(current_savings,months_salary,guess,1)
if current_savings < down_payment:
low = guess
current_savings = 0.
elif current_savings > down_payment + tolerance:
high = guess
current_savings = 0.
if (steps > 100): # I chose a maximum step number of 100 because my tolerance is high
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)
print("With current savings: ", current_savings)


Output:



down payment:  250000.0
Best saving rate: 0.656982421875
With current savings: 250072.3339667072
[Finished in 0.08s]





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Adil is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374233%2fmit-programming-in-python-problem-set-1-part-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I've already addressed most of my points in the comments, but I will recap:
    You are trying to solve a problem using a method called recursive solving, in this case using the bisection method.



    The steps are as follows:




    1. start with an initial guess you chose 0.5

    2. Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:

    3. Maximum number of steps before failure, remember we can always add 2 values and divide by 2, your result will tend to 0.999999.. otherwise

    4. A certain tolerance, if your step size is not small enough 25% of 1 M is 250 000 and you might never hit that number exactly, that' why you make a tolerance interval, for example: anything between 250 000 and 251 000 --> break loop, show result.

    5. Your if statements for changing low and high to adjust guess are correct, but you forget to re-initialize savings to 0 which means savings was going to infinity.


    Now that's all said, here's a working version of your code:



    starting_salary = 100000  # Assuming Annual Salary of 100k 
    months_salary = starting_salary/12
    total_cost = 1000000.0
    semi_annual_rate = .07
    investment_return = 0.04
    down_payment = total_cost * 0.25
    print("down payment: ", down_payment)
    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
    tolerance = down_payment/100 # I chose this tolerance to say if my savings are between [down_payment - (downpayment + down_payment/100)] result is admissible. (this is quite a high tolerance but you can change at leisure)
    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)
    return(current_savings)

    while abs(current_savings-down_payment)>=100:
    current_savings = calSavings(current_savings,months_salary,guess,1)
    if current_savings < down_payment:
    low = guess
    current_savings = 0.
    elif current_savings > down_payment + tolerance:
    high = guess
    current_savings = 0.
    if (steps > 100): # I chose a maximum step number of 100 because my tolerance is high
    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)
    print("With current savings: ", current_savings)


    Output:



    down payment:  250000.0
    Best saving rate: 0.656982421875
    With current savings: 250072.3339667072
    [Finished in 0.08s]





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      I've already addressed most of my points in the comments, but I will recap:
      You are trying to solve a problem using a method called recursive solving, in this case using the bisection method.



      The steps are as follows:




      1. start with an initial guess you chose 0.5

      2. Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:

      3. Maximum number of steps before failure, remember we can always add 2 values and divide by 2, your result will tend to 0.999999.. otherwise

      4. A certain tolerance, if your step size is not small enough 25% of 1 M is 250 000 and you might never hit that number exactly, that' why you make a tolerance interval, for example: anything between 250 000 and 251 000 --> break loop, show result.

      5. Your if statements for changing low and high to adjust guess are correct, but you forget to re-initialize savings to 0 which means savings was going to infinity.


      Now that's all said, here's a working version of your code:



      starting_salary = 100000  # Assuming Annual Salary of 100k 
      months_salary = starting_salary/12
      total_cost = 1000000.0
      semi_annual_rate = .07
      investment_return = 0.04
      down_payment = total_cost * 0.25
      print("down payment: ", down_payment)
      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
      tolerance = down_payment/100 # I chose this tolerance to say if my savings are between [down_payment - (downpayment + down_payment/100)] result is admissible. (this is quite a high tolerance but you can change at leisure)
      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)
      return(current_savings)

      while abs(current_savings-down_payment)>=100:
      current_savings = calSavings(current_savings,months_salary,guess,1)
      if current_savings < down_payment:
      low = guess
      current_savings = 0.
      elif current_savings > down_payment + tolerance:
      high = guess
      current_savings = 0.
      if (steps > 100): # I chose a maximum step number of 100 because my tolerance is high
      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)
      print("With current savings: ", current_savings)


      Output:



      down payment:  250000.0
      Best saving rate: 0.656982421875
      With current savings: 250072.3339667072
      [Finished in 0.08s]





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        I've already addressed most of my points in the comments, but I will recap:
        You are trying to solve a problem using a method called recursive solving, in this case using the bisection method.



        The steps are as follows:




        1. start with an initial guess you chose 0.5

        2. Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:

        3. Maximum number of steps before failure, remember we can always add 2 values and divide by 2, your result will tend to 0.999999.. otherwise

        4. A certain tolerance, if your step size is not small enough 25% of 1 M is 250 000 and you might never hit that number exactly, that' why you make a tolerance interval, for example: anything between 250 000 and 251 000 --> break loop, show result.

        5. Your if statements for changing low and high to adjust guess are correct, but you forget to re-initialize savings to 0 which means savings was going to infinity.


        Now that's all said, here's a working version of your code:



        starting_salary = 100000  # Assuming Annual Salary of 100k 
        months_salary = starting_salary/12
        total_cost = 1000000.0
        semi_annual_rate = .07
        investment_return = 0.04
        down_payment = total_cost * 0.25
        print("down payment: ", down_payment)
        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
        tolerance = down_payment/100 # I chose this tolerance to say if my savings are between [down_payment - (downpayment + down_payment/100)] result is admissible. (this is quite a high tolerance but you can change at leisure)
        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)
        return(current_savings)

        while abs(current_savings-down_payment)>=100:
        current_savings = calSavings(current_savings,months_salary,guess,1)
        if current_savings < down_payment:
        low = guess
        current_savings = 0.
        elif current_savings > down_payment + tolerance:
        high = guess
        current_savings = 0.
        if (steps > 100): # I chose a maximum step number of 100 because my tolerance is high
        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)
        print("With current savings: ", current_savings)


        Output:



        down payment:  250000.0
        Best saving rate: 0.656982421875
        With current savings: 250072.3339667072
        [Finished in 0.08s]





        share|improve this answer












        I've already addressed most of my points in the comments, but I will recap:
        You are trying to solve a problem using a method called recursive solving, in this case using the bisection method.



        The steps are as follows:




        1. start with an initial guess you chose 0.5

        2. Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:

        3. Maximum number of steps before failure, remember we can always add 2 values and divide by 2, your result will tend to 0.999999.. otherwise

        4. A certain tolerance, if your step size is not small enough 25% of 1 M is 250 000 and you might never hit that number exactly, that' why you make a tolerance interval, for example: anything between 250 000 and 251 000 --> break loop, show result.

        5. Your if statements for changing low and high to adjust guess are correct, but you forget to re-initialize savings to 0 which means savings was going to infinity.


        Now that's all said, here's a working version of your code:



        starting_salary = 100000  # Assuming Annual Salary of 100k 
        months_salary = starting_salary/12
        total_cost = 1000000.0
        semi_annual_rate = .07
        investment_return = 0.04
        down_payment = total_cost * 0.25
        print("down payment: ", down_payment)
        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
        tolerance = down_payment/100 # I chose this tolerance to say if my savings are between [down_payment - (downpayment + down_payment/100)] result is admissible. (this is quite a high tolerance but you can change at leisure)
        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)
        return(current_savings)

        while abs(current_savings-down_payment)>=100:
        current_savings = calSavings(current_savings,months_salary,guess,1)
        if current_savings < down_payment:
        low = guess
        current_savings = 0.
        elif current_savings > down_payment + tolerance:
        high = guess
        current_savings = 0.
        if (steps > 100): # I chose a maximum step number of 100 because my tolerance is high
        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)
        print("With current savings: ", current_savings)


        Output:



        down payment:  250000.0
        Best saving rate: 0.656982421875
        With current savings: 250072.3339667072
        [Finished in 0.08s]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 13:33









        Hadi Farah

        444212




        444212






















            Adil is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Adil is a new contributor. Be nice, and check out our Code of Conduct.













            Adil is a new contributor. Be nice, and check out our Code of Conduct.












            Adil is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374233%2fmit-programming-in-python-problem-set-1-part-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)