[Tutor] Indexing in a series for a newbie

Todd Maynard python-tutor at toddmaynard.com
Thu Jan 19 17:28:33 CET 2006


Another approach would be to change your data structure to include the words 
and hints together as tuples in the same tuple.

ie...

WORDS=( ("python","The python hint"),("program","The program hint"),
("code","The code hint") )

then you can do...

word, hint = random.choice(WORDS)


>>> WORDS=( ("python","The python hint"),("program","The program hint"),
("code","The code hint") )
>>> word, hint = random.choice(WORDS)
>>> word
'code'
>>> hint
'The code hint'
>>>


Happy coding,

-- Todd Maynard


On Thursday 19 January 2006 10:36, Jon Moore 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



More information about the Tutor mailing list