Loop until condition is true

Fredrik Lundh fredrik at pythonware.com
Tue Jun 21 10:00:36 EDT 2005


Remi Villatel wrote:

> There is always a "nice" way to do things in Python but this time I can't
> find one.
>
> What I'm trying to achieve is a conditionnal loop of which the condition
> test would be done at the end so the loop is executed at least once. It's
> some way the opposite of "while".
>
> So far, all I got is:
>
> while True:
> some(code)
> if final_condition is True:
> break
> #
> #
>
> What I don't find so "nice" is to have to build an infinite loop only to
> break it.

checking if a logical expression is true by comparing it to True is bad
style, and comparing values using "is" is also bad style.

the correct way to write that if-statement is:

    if final_condition:
        break

and yes, the "infinite" while loop is a standard Python pattern.  get
used to it.

</F>






More information about the Python-list mailing list