[Tutor] Indexing in a series for a newbie
Pujo Aji
ajikoe at gmail.com
Thu Jan 19 16:44:13 CET 2006
Hello Jon,
Why don't use randint so you can record the index and the value
Example:
WORDS = ("python", "program", "code", "xylophone")
Indword = random.randint(0,len(WORDS)-1)
word = WORDS[Indword]
print Indword
print word
Cheers,
pujo
On 1/19/06, Jon Moore <jonathan.r.moore at gmail.com> wrote:
>
> Hello
>
> 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 know that if I do:
>
> print word
>
> The randomly chosen word will be displayed. I also know that if I type:
>
> print WORDS[x]
>
> I will get the corresponding word back. But how do I find 'programaticaly'
> the index number for the string that random.choice has chosen?
>
> The reason I ask is that I an writing a simple word jumble game and need
> to link the randomly chosen word to a hint should the user need one.
>
> --
> Best Regards
>
> Jon Moore
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060119/d0688a7a/attachment.htm
More information about the Tutor
mailing list