I have a for loop nested within my while loop, what is causing an infinite loop within my code?
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 |
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 '18 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 '18 at 20:38
add a comment |
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 '18 at 20:53
Luke Paloutzian
asked Nov 20 '18 at 3:44
Luke PaloutzianLuke 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 20:38
add a comment |
1 Answer
1
active
oldest
votes
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 |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 10:46
Gautham MGautham 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.
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 '18 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 '18 at 20:38