statements in control structures (Re: Conditional Expressions don't solve the problem)

William Tanksley wtanksle at dolphin.openprojects.net
Wed Oct 24 16:11:31 EDT 2001


On Wed, 24 Oct 2001 19:46:50 +0000 (UTC), Huaiyu Zhu wrote:
>On Tue, 23 Oct 2001 22:46:56 -0600, Andrew Dalke <dalke at dalkescientific.com>
>wrote: 

>># I know, for some reason you don't like putting the end condition
>># inside the loop.  But it's one I've brought up before and it's
>># a simple mechanical transformation from what you want to do.

>The reason is very simple - the naive translation does not work.  Period.

I'm confused -- I don't see how your version can work either.  Both of you
are iterating over an empty list, and the only code which could possibly
add anything to the list is inside the iteration; that list is empty no
matter what, so nothing will execute, and the list will never be anything
but empty.

I don't like your "for x in list while y" construct, either.  It's
terrificly ambiguous: does it mean that every item in the list which meets
the 'y' criterion will be processed, or does it loop over all the items up
to the first one which fails to satisfy 'y'?

If the first, you can write more simply by saying:

for x in list:
 if y: do_things

If you meant the second, it's easier to write:

for x in list:
 if not y: break
 do_things

>Intersting.  Where have my primes gone?  :-)

The exact same place they went in your code.

>Does the above count as an example?  Can you see that the problem with the
>simple mechanical translation is that it does not handle properly the two
>different kinds of exits?

>Hint: There are three exits in the 'for p' loop.  Two of them should lead to
>the same follow-up code, the other leading to a different follow-up code.
>Finding them out is left as an exercise to the reader.  :-)

I don't see any followup code in any case -- the loop is simple and
direct.  You entire function is one big loop which runs /n/ times, but
inside that is another loop which runs 0 times.  There's no code outside
of the loops.

>Huaiyu

-- 
-William "Billy" Tanksley



More information about the Python-list mailing list