Random from Dictionary

Skip Montanaro skip at pobox.com
Thu Oct 18 19:19:14 CEST 2001


    Martin> Python 2.1.1 (#1, Jul 21 2001, 20:59:12) 
    Martin> [GCC 2.95.2 19991024 (release)] on sunos5
    Martin> Type "copyright", "credits" or "license" for more information.
    >>>> x={'Hallo':'Welt'}
    >>>> import random
    >>>> random.choice(x)
    Martin> Traceback (most recent call last):
    Martin>   File "<stdin>", line 1, in ?
    Martin>   File "/usr/local/lib/python2.1/random.py", line 329, in choice
    Martin>     return seq[int(self.random() * len(seq))]
    Martin> KeyError: 0

Perhaps I'm coming in on this thread a bit late (don't remember seeing it
and can't find it at Google Groups, however, so maybe it just hasn't fully
propagated around), but I see nothing in the doc string for random.choice
that suggests it should work with a dictionary.  Where's the bug?

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list