[Tutor] continuous running of a method
Greg Bair
gregbair at gmail.com
Mon Aug 23 07:41:28 CEST 2010
On 08/23/2010 01:10 AM, James Mills wrote:
> On Mon, Aug 23, 2010 at 3:00 PM, Greg Bair<gregbair at gmail.com> wrote:
>
>> I have a method (I'll call it foo) that will either return None or an object
>> depending on a random value generated. What I want to happen is that if I
>> call foo(), i.e, f = foo() and it returns None, to re-call it until it
>> returns something else. I would think this would be done with a while loop,
>> but can't seem to get the details right.
>>
>> Any help would be greatly appreciated.
>>
> Quite simple really, and yes you are right. Use a while loop:
>
>
>>>> from random import seed, random
>>>> from time import time
>>>> seed(time())
>>>> def foo():
>>>>
> ... if 0.5< random()< 0.6:
> ... return True
> ...
>
>>>> x = foo()
>>>> while x is None:
>>>>
> ... x = foo()
> ...
>
>>>> x
>>>>
> True
>
>>>>
> cheers
> James
>
>
I don't know why I didn't think of that. Thanks.
More information about the Tutor
mailing list