[Python-ideas] "While" suggestion

BJörn Lindqvist bjourne at gmail.com
Thu Jul 3 22:53:59 CEST 2008

On Thu, Jul 3, 2008 at 2:58 PM, Thomas Lee <tom at vector-seven.com> wrote:
> Stavros Korokithakis wrote:
>> I don't think they do, if I'm not mistaken the only way is to call read()
>> and see if it returns the empty string. I agree that this would be better,
>> but the use case I mentioned is not the only one this would be useful in...
>> Unfortunately I can't think of any right now,
> :)
>> but there have been a few times when I had to initialise things outside
>> the loop and it always strikes me as ugly.
> Well that depends, on the situation really. The only use case I can think of
> is exactly the one you mentioned. And since you can't think of any other
> scenarios where such a thing might be handy, I've got no better suggestion
> to offer.
> If you can conjure up another scenario, post it back here and we'll see if
> we can generalize the pattern a little.

Randomly choose a number 0-100 that is not already picked:

# Current Python
number = random.randint(1, 100)
while number in picked_numbers:
    number = random.randint(1, 100)

# Do-Until
    number = random.randint(1, 100)
until number not in picked_numbers

# While-As
Doesn't seem to be doable. Maybe with the proposed syntax either:

while random.randint(1, 100) as number in picked_numbers:


while random.randint(1, 100) in picked_numbers as number:

would work. But it doesn't look pretty anymore. I would definitely
prefer a do-until or do-while construct instead.

mvh Björn

More information about the Python-ideas mailing list