[Tutor] A puzzle for you

John Fouhy john at fouhy.net
Tue Oct 17 23:21:53 CEST 2006


On 18/10/06, Terry Carroll <carroll at tjc.com> wrote:
> On Sat, 14 Oct 2006, John Fouhy wrote:
>
> > >From the python_dev LiveJournal community ---
> >
> > Predict the outcome of the following code:
> >
> > ##
> > from random import *
> > seed()
> > [choice(dict((x+1,0) for x in range(1000))) for x in range(693)][0]
> > ##
>
> On ActivePython 2.4.3:
>
> >>> from random import *
> >>> seed()
> >>> [choice(dict((x+1,0) for x in range(1000))) for x in range(693)][0]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "C:\Python24\Lib\random.py", line 249, in choice
>     return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
> KeyError: 0
> >>>

Does that always happen?

-- 
John.


More information about the Tutor mailing list