[Tutor] I don't understand this code

Stefan Behnel stefan_ml at behnel.de
Wed Jul 14 12:40:18 CEST 2010


ZUXOXUS, 13.07.2010 16:43:
> Hi,
>
> I am a true beginner in programming, and im learning with
> inventwithpython.com.
>
> There's something I dont understand, and i would really appreciate any help.
>
> In chapter 9, the one about the Hangman game, I don't get the block of code
> in line 61
>
> 59.  words = 'ant baboon badger bat bear'
> 60.
>
>     1. def getRandomWord(wordList):
>     2.     # This function returns a random string from the passed list of
>     strings.
>     3.     wordIndex = random.randint(0, len(wordList) - 1)
>     4.     return wordList[wordIndex]
>
>
> The thing is, the "passed list of strings" is called "words", not
> "wordList", so I see it shouldn't work.

I'm pretty sure that's not how it's called. However, you can transform the 
whitespace separated string into a list by calling .split() on it:

     wordList = words.split()

Also, the function above is redundant with the stdlib, use random.choice() 
instead, which does exactly the same thing.

Stefan



More information about the Tutor mailing list