On Thu, 19 Jan 2006, Jon Moore wrote:

> I need some help for a program I am writing as a newbie to Python.
>
> I have created a series:
>
> WORDS = ("python", "program", "code", "xylophone")
>
> and then assigned one of them randomly to the variable 'word':
>
> word = random.choice(WORDS)
>
> I will get the corresponding word back. But how do I find 'programaticaly'
> the index number for the string that random.choice has chosen?

Hi Jon,

If you need the index as well as the sequence element, then it may be more
practical to select a random index rather than a random element.  The
reason is because it's easy to get a sequence element from its index, but
not necessarily visa-versa.

The random.randrange() function may be useful here.

Good luck!

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to