[Python-ideas] "While" suggestion

Chris Rebert cvrebert at gmail.com
Thu Jul 3 23:01:35 CEST 2008


Purely for the sake of completeness:

#while True idiom (current Python)
while True:
    number = random.randint(1, 100)
    if number not in picked_numbers:
        break


- Chris

On Thu, Jul 3, 2008 at 1:53 PM, BJörn Lindqvist <bjourne at gmail.com> wrote:
> 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
> do:
>    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:
>    pass
>
> or
>
> while random.randint(1, 100) in picked_numbers as number:
>    pass
>
> would work. But it doesn't look pretty anymore. I would definitely
> prefer a do-until or do-while construct instead.
>
>
> --
> mvh Björn
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>


More information about the Python-ideas mailing list