[Tutor] A puzzle for you

Terry Carroll carroll at tjc.com
Tue Oct 17 23:15:44 CEST 2006


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
>>>




More information about the Tutor mailing list