• No results found

Code Explanation continued 3 HANGMANPICS = ['''

4. 5. +---+ 6. | | 7. | | 8. | 9. | 10. | 11. | 12. | 13. | 14. | 15. | 16. ==============''', '''

...the rest of the code is too big to show here...

variable HANGMANPICS is a list of multi-line strings. Each multi-line string in this list will be the picture (in ASCII art) of the hangman board. The string at HANGMANPICS[0] is the hangman's noose with no body parts. The string at HANGMANPICS[1] has just the head, HANGMANPICS[2] has the head and body, and so on.

98. words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite mole monkey moose moth mouse mule newt

otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()

Line 98 assigns a list to the variable words. This will be the list of all possible secret words in this game. The secret word will be selected from this list. All of the possible secret words are some kind of animal (so the player has some idea what the word is).

But the value being assigned to words doesn't look like a list. It does not have the [ and ] square brackets. But there is a special kind of function call at the end of the long string, .split(). This is a method on the string, and it will evaluate to a list which is then stored in words. Read on to find out what methods are.

Methods

Methods are functions that are attached with a certain value. For example, the strings have a lower () method. You cannot just call the lower() by itself. You must attach the method call to a specific string. Try typing 'Hello world!'.lower() into the interactive shell:

The lower() method returns the lowercase version of the string it is attached to. There is also an

Because the upper() method returns a string, you can call a method on that string as well. Try typing 'Hello world!'.upper().lower() into the shell:

'Hello world!'.upper() evaluates to the string 'HELLO WORLD!', and then we call that string's lower() method. This returns the string 'hello world!', which is the final value in the evaluation. The order is important. 'Hello world!'.lower().upper() is not the same as

'Hello world!'.upper().lower():

Remember, if a string is stored in a variable, you can call a string method on that variable. Look at this example:

The list data type also has methods. The reverse() method will reverse the order of the items in the list. Try typing spam = [1, 2, 3, 4, 5, 6, 'meow', 'woof'] and then

spam.reverse() (to reverse the list). Then type spam to view the contents of the spam variable.

The most common list method you will use is append(). This method will add the value you pass as an argument to the end of the list. Try typing the following into the shell:

eggs = [] eggs.append('hovercraft') eggs eggs.append('eels') eggs eggs.append(42) eggs

While strings and lists have methods, integers do not happen to have any methods.

You may be wondering why Python has methods anyway, since they do the same thing as functions. Attaching functions to values (which is what methods are) becomes a lot more useful in object-oriented programming (OOP). Strings and lists are also known as a special type of data type called objects. But object-oriented programming is a bit advanced for this book, and you don't need to know OOP to make these games. You only need to know about string methods and the append() list method.

Code Explanation continued...

98. words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite mole monkey moose moth mouse mule newt

otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()

As you can see, this line is just one very, very long string that has the split() method called on it. The split() method will return a list made up of the words in the string that are separated by a space.

(The string is split up into a list of items.) The reason we do it this way instead of just writing out the list is that it is easier to type as one long string. Otherwise you would have to type: ['ant',

'baboon', 'badger',... with all the quotes and commas. The words list will contain the possible secret words that can show up in the game. You can add or remove your own words to this string later if you want to change the words used in the Hangman game.

For an example of how the split() string method works, try typing 'My very energetic mother just served us nine pies'.split() into the shell:

The result is a list of nine strings, one string for each of the words in the original string. The spaces are dropped from the items in the list.

100. def getRandomWord(wordList):

101. # This function returns a random string from the passed list of strings.

102. wordIndex = random.randint(0, len(wordList) - 1) 103. return wordList[wordIndex]

Starting on line 100, we define a new function called getRandomWord() which has a single parameter named wordList. We will call this function when we want to pick a secret word from a list of secret words. This function makes use of a new Python function named len(), which I will explain first.