开发者

Python loop that always executes at least once? [duplicate]

This question already has answers here: 开发者_开发知识库 Closed 11 years ago.

Possible Duplicate:

do-while loop in Python?

How do I write a loop in Python that will always be executed at least once, with the test being performed after the first iteration? (different to the 20 or so python examples of for and while loops I found through google and python docs)


while True:
    #loop body
    if (!condition): break


You could try:

def loop_body():
    # implicitly return None, which is false-ish, at the end


while loop_body() or condition: pass

But realistically I think I would do it the other way. In practice, you don't really need it that often anyway. (Even less often than you think; try to refactor in other ways.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜