MIT programming in python Problem Set 1 Part C
up vote
-4
down vote
favorite
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.
python python-3.x
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.
|
show 2 more comments
up vote
-4
down vote
favorite
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.
python python-3.x
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 neithercurrent_savingsnordown_paymentare 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.") breakinstead ofelse: 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
|
show 2 more comments
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
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.
python python-3.x
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
- 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.
python python-3.x
python python-3.x
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.
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 neithercurrent_savingsnordown_paymentare 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.") breakinstead ofelse: 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
|
show 2 more comments
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 neithercurrent_savingsnordown_paymentare 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.") breakinstead ofelse: 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
|
show 2 more comments
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:
- start with an initial
guessyou chose0.5
- Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:
- 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
- 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.
- Your if statements for changing
lowandhighto 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]
add a comment |
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:
- start with an initial
guessyou chose0.5
- Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:
- 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
- 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.
- Your if statements for changing
lowandhighto 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]
add a comment |
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:
- start with an initial
guessyou chose0.5
- Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:
- 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
- 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.
- Your if statements for changing
lowandhighto 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]
add a comment |
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:
- start with an initial
guessyou chose0.5
- Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:
- 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
- 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.
- Your if statements for changing
lowandhighto 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]
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:
- start with an initial
guessyou chose0.5
- Perform your calculations in a loop and iterate the initial guess, in this case you must account for the following:
- 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
- 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.
- Your if statements for changing
lowandhighto 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]
answered Nov 19 at 13:33
Hadi Farah
444212
444212
add a comment |
add a comment |
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.
Adil is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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_savingsnordown_paymentare 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.") breakinstead ofelse: 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