PEP 315: Enhanced While Loop

Dan Bishop danb_83 at yahoo.com
Sat May 3 05:36:43 EDT 2003


"Ben Allfree" <benles at bldigital.com> wrote in message news:<mailman.1051935786.17713.python-list at python.org>...
> Can you provide an example snippit?
> 
> I'm interested, but I can't come up with a scenario where the loop couldn't
> be rewritten, except for do..until loops that must execute at least once,
> which require the first loop iteration to be placed before a while loop.

After grepping through a few thousand lines of Python code, I found
only one use of the "while 1"..."break" construct (which I assume is
what do...while intends to replace), which was the main loop in my
text adventure game.

   while 1:
      # ...
      if not getYN("You are now dead.  Would you like to play again
(Y/N)?"):
         break
      if getYN("Would you like to read the intro again (Y/N)?"):
         intro()

I'm not sure what the equivalent do...while loop would be.
 
> And for that scenario, I would propose a simple "until" loop construct that
> is guaranteed to execute once before evaluation:
> 
> # Old way
> MakeMoney()
> while Poor():
>    MakeMoney()
> 
> # New way
> until not Poor():
>    MakeMoney()
> 
> The above example assumes one must MakeMoney() before running the Poor()
> test ;)
> 
> Python programmers would just have to understand that the "until" construct
> executes once before evaluation. Or maybe it could be named something more
> obvious. "AfterWhile" - heh - no pun intended.

TI-8x BASIC (the only language I know of that puts posttest loop
conditions at the beginning) uses

:Repeat not Poor()
:MakeMoney
:End

(Actually, this isn't valid code, because there are no user-defined
functions and no identifiers longer than 8 chars, but this is
irrelevant.)

In future Python, this could be written

repeat not Poor():
   MakeMoney()

My personal preferred syntax is

posttest while Poor():
   MakeMoney()




More information about the Python-list mailing list