I have a for loop nested within my while loop, what is causing an infinite loop within my code?
up vote
0
down vote
favorite
I'm new to Python and coding in general. I am trying to use a while loop with a nested for loop to calculate the lowest % of salary that can be saved that will reach the savings goal within 36 months. The code also includes functions to apply a semi-annual raise to the salary every 6 months and to apply interest gained to the savings.
When I run my code it results in an infinite loop and I haven't been able to see what is causing it.
total_cost = 1000000
semi_annual_raise = 0.07
down_payment = 0.25*total_cost
starting_salary = float(input("What is your annual salary?: "))
monthly_salary = starting_salary/12.0
r = 0.04
steps = 0
epsilon = 100
low = 0
high = 10000
portion_saved = (high + low)/2.0
current_savings = 0
raise_counter = 0
while abs(current_savings - down_payment) > epsilon:
current_savings = 0
for months in range(36):
current_savings += (current_savings*r/12) + (monthly_salary*
(portion_saved/10000))
if raise_counter == 6:
monthly_salary += monthly_salary*semi_annual_raise
raise_counter = 0
raise_counter += 1
if current_savings < down_payment:
low = portion_saved
else:
high = portion_saved
portion_saved = (high + low)/2.0
steps += 1
raise_counter = 0
print ("Number of steps =", steps)
print ("Optimal % to save:", portion_saved/10000)
python-3.x for-loop if-statement while-loop bisection
add a comment |
up vote
0
down vote
favorite
I'm new to Python and coding in general. I am trying to use a while loop with a nested for loop to calculate the lowest % of salary that can be saved that will reach the savings goal within 36 months. The code also includes functions to apply a semi-annual raise to the salary every 6 months and to apply interest gained to the savings.
When I run my code it results in an infinite loop and I haven't been able to see what is causing it.
total_cost = 1000000
semi_annual_raise = 0.07
down_payment = 0.25*total_cost
starting_salary = float(input("What is your annual salary?: "))
monthly_salary = starting_salary/12.0
r = 0.04
steps = 0
epsilon = 100
low = 0
high = 10000
portion_saved = (high + low)/2.0
current_savings = 0
raise_counter = 0
while abs(current_savings - down_payment) > epsilon:
current_savings = 0
for months in range(36):
current_savings += (current_savings*r/12) + (monthly_salary*
(portion_saved/10000))
if raise_counter == 6:
monthly_salary += monthly_salary*semi_annual_raise
raise_counter = 0
raise_counter += 1
if current_savings < down_payment:
low = portion_saved
else:
high = portion_saved
portion_saved = (high + low)/2.0
steps += 1
raise_counter = 0
print ("Number of steps =", steps)
print ("Optimal % to save:", portion_saved/10000)
python-3.x for-loop if-statement while-loop bisection
current_savings
goes only up anddown_payment
does not change? In this case the conditionabs(current_savings - down_payment) > epsilon
will beTrue
forever.
– Poolka
Nov 20 at 6:58
@Poolka How do I reset current_savings to 0 for the beginning of each iteration of the while loop?
– Luke Paloutzian
Nov 20 at 20:38
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm new to Python and coding in general. I am trying to use a while loop with a nested for loop to calculate the lowest % of salary that can be saved that will reach the savings goal within 36 months. The code also includes functions to apply a semi-annual raise to the salary every 6 months and to apply interest gained to the savings.
When I run my code it results in an infinite loop and I haven't been able to see what is causing it.
total_cost = 1000000
semi_annual_raise = 0.07
down_payment = 0.25*total_cost
starting_salary = float(input("What is your annual salary?: "))
monthly_salary = starting_salary/12.0
r = 0.04
steps = 0
epsilon = 100
low = 0
high = 10000
portion_saved = (high + low)/2.0
current_savings = 0
raise_counter = 0
while abs(current_savings - down_payment) > epsilon:
current_savings = 0
for months in range(36):
current_savings += (current_savings*r/12) + (monthly_salary*
(portion_saved/10000))
if raise_counter == 6:
monthly_salary += monthly_salary*semi_annual_raise
raise_counter = 0
raise_counter += 1
if current_savings < down_payment:
low = portion_saved
else:
high = portion_saved
portion_saved = (high + low)/2.0
steps += 1
raise_counter = 0
print ("Number of steps =", steps)
print ("Optimal % to save:", portion_saved/10000)
python-3.x for-loop if-statement while-loop bisection
I'm new to Python and coding in general. I am trying to use a while loop with a nested for loop to calculate the lowest % of salary that can be saved that will reach the savings goal within 36 months. The code also includes functions to apply a semi-annual raise to the salary every 6 months and to apply interest gained to the savings.
When I run my code it results in an infinite loop and I haven't been able to see what is causing it.
total_cost = 1000000
semi_annual_raise = 0.07
down_payment = 0.25*total_cost
starting_salary = float(input("What is your annual salary?: "))
monthly_salary = starting_salary/12.0
r = 0.04
steps = 0
epsilon = 100
low = 0
high = 10000
portion_saved = (high + low)/2.0
current_savings = 0
raise_counter = 0
while abs(current_savings - down_payment) > epsilon:
current_savings = 0
for months in range(36):
current_savings += (current_savings*r/12) + (monthly_salary*
(portion_saved/10000))
if raise_counter == 6:
monthly_salary += monthly_salary*semi_annual_raise
raise_counter = 0
raise_counter += 1
if current_savings < down_payment:
low = portion_saved
else:
high = portion_saved
portion_saved = (high + low)/2.0
steps += 1
raise_counter = 0
print ("Number of steps =", steps)
print ("Optimal % to save:", portion_saved/10000)
python-3.x for-loop if-statement while-loop bisection
python-3.x for-loop if-statement while-loop bisection
edited Nov 20 at 20:53
asked Nov 20 at 3:44
Luke Paloutzian
11
11
current_savings
goes only up anddown_payment
does not change? In this case the conditionabs(current_savings - down_payment) > epsilon
will beTrue
forever.
– Poolka
Nov 20 at 6:58
@Poolka How do I reset current_savings to 0 for the beginning of each iteration of the while loop?
– Luke Paloutzian
Nov 20 at 20:38
add a comment |
current_savings
goes only up anddown_payment
does not change? In this case the conditionabs(current_savings - down_payment) > epsilon
will beTrue
forever.
– Poolka
Nov 20 at 6:58
@Poolka How do I reset current_savings to 0 for the beginning of each iteration of the while loop?
– Luke Paloutzian
Nov 20 at 20:38
current_savings
goes only up and down_payment
does not change? In this case the condition abs(current_savings - down_payment) > epsilon
will be True
forever.– Poolka
Nov 20 at 6:58
current_savings
goes only up and down_payment
does not change? In this case the condition abs(current_savings - down_payment) > epsilon
will be True
forever.– Poolka
Nov 20 at 6:58
@Poolka How do I reset current_savings to 0 for the beginning of each iteration of the while loop?
– Luke Paloutzian
Nov 20 at 20:38
@Poolka How do I reset current_savings to 0 for the beginning of each iteration of the while loop?
– Luke Paloutzian
Nov 20 at 20:38
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Why have you used (monthly_salary*portion_saved/10000
? Since you need to add the portion_saved
to the current_savings
each month, i would recommend you to use
current_savings+=(current_savings*r/12) + portion_saved
I think you can also simplify the process by finding the % to be saved using the below formula
monthly_salary*(save_percent/100)* 36 = down_payment-epsilon
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
Why have you used (monthly_salary*portion_saved/10000
? Since you need to add the portion_saved
to the current_savings
each month, i would recommend you to use
current_savings+=(current_savings*r/12) + portion_saved
I think you can also simplify the process by finding the % to be saved using the below formula
monthly_salary*(save_percent/100)* 36 = down_payment-epsilon
add a comment |
up vote
0
down vote
Why have you used (monthly_salary*portion_saved/10000
? Since you need to add the portion_saved
to the current_savings
each month, i would recommend you to use
current_savings+=(current_savings*r/12) + portion_saved
I think you can also simplify the process by finding the % to be saved using the below formula
monthly_salary*(save_percent/100)* 36 = down_payment-epsilon
add a comment |
up vote
0
down vote
up vote
0
down vote
Why have you used (monthly_salary*portion_saved/10000
? Since you need to add the portion_saved
to the current_savings
each month, i would recommend you to use
current_savings+=(current_savings*r/12) + portion_saved
I think you can also simplify the process by finding the % to be saved using the below formula
monthly_salary*(save_percent/100)* 36 = down_payment-epsilon
Why have you used (monthly_salary*portion_saved/10000
? Since you need to add the portion_saved
to the current_savings
each month, i would recommend you to use
current_savings+=(current_savings*r/12) + portion_saved
I think you can also simplify the process by finding the % to be saved using the below formula
monthly_salary*(save_percent/100)* 36 = down_payment-epsilon
answered Nov 20 at 10:46
Gautham M
22619
22619
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53385904%2fi-have-a-for-loop-nested-within-my-while-loop-what-is-causing-an-infinite-loop%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
current_savings
goes only up anddown_payment
does not change? In this case the conditionabs(current_savings - down_payment) > epsilon
will beTrue
forever.– Poolka
Nov 20 at 6:58
@Poolka How do I reset current_savings to 0 for the beginning of each iteration of the while loop?
– Luke Paloutzian
Nov 20 at 20:38