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

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Oct 24 15:46:50 EDT 2001


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.

># And only one extra line (assuming your proposed code is written in
># a more readable form.)
>def getprimes(x):
>  primes = []
>  for n in range(2, x):
>    for p in primes:
>      if p*p > n:
>        primes.append(n)
>        break
>      elif n % p == 0:
>        break
>  return primes

Intersting.  Where have my primes gone?  :-)

>>> getprimes (3)
[]
>>> getprimes (4)
[]
>>> getprimes (100)
[]

>>Thinking about this - perhaps the real problem is multiple exits for
>loops -
>>at least one normal end to loop and another abnormal end.
>
>And I still insist there is no problem.  You've yet to really
>come up with an example of how your proposal would clarify any
>real code, and you have yet to show that the addition of a new
>construct won't make Python more confusing.  Shortness does not
>equal readable.
>

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.  :-)

BTW, I'm not insisting on new syntax now - at least not until there is one
that can handle all thes cases in a straightforward manner.  I can see that
iterators can solve some of them, but not all.  The problem is more
complicted than either you or I thought initially.

Huaiyu



More information about the Python-list mailing list