[Tutor] newbie code review please!
Kent Johnson
kent37 at tds.net
Mon Feb 4 17:56:17 CET 2008
Tyler Smith wrote:
> That cleaned up a lot. However, I couldn't figure out a way to do
> random.choice(word_hash[(w1, w2)]) on a dict with set-type values.
Ah, sorry; random.choice() needs an indexable sequence. Try
w1, w2 = w2, random.sample(word_hash[(w1, w2)], 1)[0]
random.sample() works with any iterable, not just sequences. It returns
a list, hence the indexing [0].
Kent
More information about the Tutor
mailing list