Start Python 'while' loop before condition is defined [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Can we have assignment in a condition?
9 answers
Emulate a do-while loop in Python?
13 answers
I've got a piece of code that executes a function, and then based on that output decides if it should reiterate:
while (function_output) > tolerance:
function_output = function(x)
The problem is that the while loop won't start until "function_output" is defined - but it's defined in the loop. Currenly I've got:
function_output = function(x)
while (function_output) > tolerance:
function_output = function(x)
but is there a way to get this loop to start without having to iterate the function once already?
python
marked as duplicate by juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 16:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 6 more comments
This question already has an answer here:
Can we have assignment in a condition?
9 answers
Emulate a do-while loop in Python?
13 answers
I've got a piece of code that executes a function, and then based on that output decides if it should reiterate:
while (function_output) > tolerance:
function_output = function(x)
The problem is that the while loop won't start until "function_output" is defined - but it's defined in the loop. Currenly I've got:
function_output = function(x)
while (function_output) > tolerance:
function_output = function(x)
but is there a way to get this loop to start without having to iterate the function once already?
python
marked as duplicate by juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 16:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Emulate ado while?while True: stuff() if fail_condition: break
– Robert Harvey♦
Nov 23 '18 at 16:36
No, python has no "do"-"while" loop
– Azat Ibrakov
Nov 23 '18 at 16:37
function_output = tolerance + 1 before the loop?
– warped
Nov 23 '18 at 16:37
3
@TIF: For what it's worth, your second block of code is the canonical way to do it.
– Robert Harvey♦
Nov 23 '18 at 16:38
1
The OP is correct, in thinking that the 2nd way is bad: It has repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:41
|
show 6 more comments
This question already has an answer here:
Can we have assignment in a condition?
9 answers
Emulate a do-while loop in Python?
13 answers
I've got a piece of code that executes a function, and then based on that output decides if it should reiterate:
while (function_output) > tolerance:
function_output = function(x)
The problem is that the while loop won't start until "function_output" is defined - but it's defined in the loop. Currenly I've got:
function_output = function(x)
while (function_output) > tolerance:
function_output = function(x)
but is there a way to get this loop to start without having to iterate the function once already?
python
This question already has an answer here:
Can we have assignment in a condition?
9 answers
Emulate a do-while loop in Python?
13 answers
I've got a piece of code that executes a function, and then based on that output decides if it should reiterate:
while (function_output) > tolerance:
function_output = function(x)
The problem is that the while loop won't start until "function_output" is defined - but it's defined in the loop. Currenly I've got:
function_output = function(x)
while (function_output) > tolerance:
function_output = function(x)
but is there a way to get this loop to start without having to iterate the function once already?
This question already has an answer here:
Can we have assignment in a condition?
9 answers
Emulate a do-while loop in Python?
13 answers
python
python
asked Nov 23 '18 at 16:35
TIFTIF
264
264
marked as duplicate by juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 16:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by juanpa.arrivillaga
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 16:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Emulate ado while?while True: stuff() if fail_condition: break
– Robert Harvey♦
Nov 23 '18 at 16:36
No, python has no "do"-"while" loop
– Azat Ibrakov
Nov 23 '18 at 16:37
function_output = tolerance + 1 before the loop?
– warped
Nov 23 '18 at 16:37
3
@TIF: For what it's worth, your second block of code is the canonical way to do it.
– Robert Harvey♦
Nov 23 '18 at 16:38
1
The OP is correct, in thinking that the 2nd way is bad: It has repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:41
|
show 6 more comments
2
Emulate ado while?while True: stuff() if fail_condition: break
– Robert Harvey♦
Nov 23 '18 at 16:36
No, python has no "do"-"while" loop
– Azat Ibrakov
Nov 23 '18 at 16:37
function_output = tolerance + 1 before the loop?
– warped
Nov 23 '18 at 16:37
3
@TIF: For what it's worth, your second block of code is the canonical way to do it.
– Robert Harvey♦
Nov 23 '18 at 16:38
1
The OP is correct, in thinking that the 2nd way is bad: It has repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:41
2
2
Emulate a
do while? while True: stuff() if fail_condition: break– Robert Harvey♦
Nov 23 '18 at 16:36
Emulate a
do while? while True: stuff() if fail_condition: break– Robert Harvey♦
Nov 23 '18 at 16:36
No, python has no "do"-"while" loop
– Azat Ibrakov
Nov 23 '18 at 16:37
No, python has no "do"-"while" loop
– Azat Ibrakov
Nov 23 '18 at 16:37
function_output = tolerance + 1 before the loop?
– warped
Nov 23 '18 at 16:37
function_output = tolerance + 1 before the loop?
– warped
Nov 23 '18 at 16:37
3
3
@TIF: For what it's worth, your second block of code is the canonical way to do it.
– Robert Harvey♦
Nov 23 '18 at 16:38
@TIF: For what it's worth, your second block of code is the canonical way to do it.
– Robert Harvey♦
Nov 23 '18 at 16:38
1
1
The OP is correct, in thinking that the 2nd way is bad: It has repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:41
The OP is correct, in thinking that the 2nd way is bad: It has repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:41
|
show 6 more comments
3 Answers
3
active
oldest
votes
There is no such thing in python. Neither a do-while, nor a
while (x = f() > 5):
dostuff
like in C and similar languages.
Similar constructs have been proposed, but rejected. What you are already doing is the best way to do it.
On the other hand, if you want to do it in a do-while style, the proposed way is
while True:
if f() > tolerance:
break
1
soon
– timgeb
Nov 23 '18 at 16:49
@timgeb: useful (though a bit ugly looking). Let's hope for ado-whiletoo...
– blue_note
Nov 23 '18 at 16:52
add a comment |
Use a break statement to escape from a loop
while True:
if function_output(x) > tolerance:
break
2
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
add a comment |
How about this pattern. You may want to choose a better variable name.
should_continue = True
while should_continue:
should_continue = ( function(x) > tolerance )
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
1
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
1
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no such thing in python. Neither a do-while, nor a
while (x = f() > 5):
dostuff
like in C and similar languages.
Similar constructs have been proposed, but rejected. What you are already doing is the best way to do it.
On the other hand, if you want to do it in a do-while style, the proposed way is
while True:
if f() > tolerance:
break
1
soon
– timgeb
Nov 23 '18 at 16:49
@timgeb: useful (though a bit ugly looking). Let's hope for ado-whiletoo...
– blue_note
Nov 23 '18 at 16:52
add a comment |
There is no such thing in python. Neither a do-while, nor a
while (x = f() > 5):
dostuff
like in C and similar languages.
Similar constructs have been proposed, but rejected. What you are already doing is the best way to do it.
On the other hand, if you want to do it in a do-while style, the proposed way is
while True:
if f() > tolerance:
break
1
soon
– timgeb
Nov 23 '18 at 16:49
@timgeb: useful (though a bit ugly looking). Let's hope for ado-whiletoo...
– blue_note
Nov 23 '18 at 16:52
add a comment |
There is no such thing in python. Neither a do-while, nor a
while (x = f() > 5):
dostuff
like in C and similar languages.
Similar constructs have been proposed, but rejected. What you are already doing is the best way to do it.
On the other hand, if you want to do it in a do-while style, the proposed way is
while True:
if f() > tolerance:
break
There is no such thing in python. Neither a do-while, nor a
while (x = f() > 5):
dostuff
like in C and similar languages.
Similar constructs have been proposed, but rejected. What you are already doing is the best way to do it.
On the other hand, if you want to do it in a do-while style, the proposed way is
while True:
if f() > tolerance:
break
edited Nov 23 '18 at 16:41
answered Nov 23 '18 at 16:40
blue_noteblue_note
12.7k32536
12.7k32536
1
soon
– timgeb
Nov 23 '18 at 16:49
@timgeb: useful (though a bit ugly looking). Let's hope for ado-whiletoo...
– blue_note
Nov 23 '18 at 16:52
add a comment |
1
soon
– timgeb
Nov 23 '18 at 16:49
@timgeb: useful (though a bit ugly looking). Let's hope for ado-whiletoo...
– blue_note
Nov 23 '18 at 16:52
1
1
soon
– timgeb
Nov 23 '18 at 16:49
soon
– timgeb
Nov 23 '18 at 16:49
@timgeb: useful (though a bit ugly looking). Let's hope for a
do-while too...– blue_note
Nov 23 '18 at 16:52
@timgeb: useful (though a bit ugly looking). Let's hope for a
do-while too...– blue_note
Nov 23 '18 at 16:52
add a comment |
Use a break statement to escape from a loop
while True:
if function_output(x) > tolerance:
break
2
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
add a comment |
Use a break statement to escape from a loop
while True:
if function_output(x) > tolerance:
break
2
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
add a comment |
Use a break statement to escape from a loop
while True:
if function_output(x) > tolerance:
break
Use a break statement to escape from a loop
while True:
if function_output(x) > tolerance:
break
edited Nov 23 '18 at 16:50
answered Nov 23 '18 at 16:38
TobeyTobey
1,0491821
1,0491821
2
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
add a comment |
2
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
2
2
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
I think your condition is inverted - that was the condition to continue the loop in the OPs code
– SpoonMeiser
Nov 23 '18 at 16:40
add a comment |
How about this pattern. You may want to choose a better variable name.
should_continue = True
while should_continue:
should_continue = ( function(x) > tolerance )
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
1
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
1
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
add a comment |
How about this pattern. You may want to choose a better variable name.
should_continue = True
while should_continue:
should_continue = ( function(x) > tolerance )
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
1
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
1
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
add a comment |
How about this pattern. You may want to choose a better variable name.
should_continue = True
while should_continue:
should_continue = ( function(x) > tolerance )
How about this pattern. You may want to choose a better variable name.
should_continue = True
while should_continue:
should_continue = ( function(x) > tolerance )
answered Nov 23 '18 at 16:39
ctrl-alt-delorctrl-alt-delor
4,33732644
4,33732644
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
1
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
1
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
add a comment |
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
1
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
1
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
While this is certainly an alternative, it isn't much better than OP's implementation.
– Idlehands
Nov 23 '18 at 16:41
1
1
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
@Idlehands is has less repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:42
1
1
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@ctrl-alt-delor, What's repeated, a function call? If I had to weight up a repeated function call versus creating a new variable, I know which I'd prefer.
– jpp
Nov 23 '18 at 16:43
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
@Jpp I agree if just a function call, but if there are lots or arguments, the I would do it this way. Or encapsulate.
– ctrl-alt-delor
Nov 23 '18 at 17:07
add a comment |
2
Emulate a
do while?while True: stuff() if fail_condition: break– Robert Harvey♦
Nov 23 '18 at 16:36
No, python has no "do"-"while" loop
– Azat Ibrakov
Nov 23 '18 at 16:37
function_output = tolerance + 1 before the loop?
– warped
Nov 23 '18 at 16:37
3
@TIF: For what it's worth, your second block of code is the canonical way to do it.
– Robert Harvey♦
Nov 23 '18 at 16:38
1
The OP is correct, in thinking that the 2nd way is bad: It has repeated code.
– ctrl-alt-delor
Nov 23 '18 at 16:41