Chapter 6 - Hangman

This game introduces many new concepts. But don't worry, we'll experiment with these programming concepts in the interactive shell first. Once you understand these concepts, then it will be much easier to understand our next game, Hangman.

In case you don't know, Hangman is a game two people play with paper and pencil. One person thinks of a word, and then draws blanks for each letter in the word. The other person guesses letters that might be in the word. If they guess correctly, the first person writes the letter into the blank. If they guess wrong, the first person draws another body part of the hangman. If the second person can guess all the letters in the word before the hangman has completely been drawn, they win.

ASCII Art

This code is about four times larger than our Dragon World game! But don't worry. Half of the lines of code aren't really code at all, but are strings that use keyboard characters to draw pictures. This type of graphics is called ASCII art (pronounced "ask-ee"), because keyboard characters (such as letters, numbers, and also all the other signs on the keyboard) are called ASCII characters. ASCII stands for American Standard Code for Information Interchange. Here are some cats (with the artist's initials) done in ASCII art:

                       ,')
                      ( (
                       ) )
                      / /
                     / /
                  ,-'  `-.
                ,'        \
     _    , _,-'    (      )
     )`--/,)         `.   (
    /      \ (         )  /
   6),6>    ) \     )_/  /
 __(Y:.  _,'   )   /(((_/
(((_^---'`._,-'   /  ```   hjw
'''       ((( _.-'
          '''
    /\-/\
   /a a  \                  _
  =\ Y  =/-~~~~~~-,________/ )
    '^--'          _________/
      \           /
      ||  |---'\  \   
jgs  (_(__|   ((__|   
  (\(\
  ).. \
  \Y_, '-.
    )     '.
    |  \/   \ 
    \\ |\_  |_
jgs ((_/(__/_,'.
         (,----'

So this program's code is only about twice the size of Dragon World (if you don't count the pictures). Go ahead and type in this code into the file editor, and save the file as hangman.py. Then run the program by pressing F5. It might be a good idea to save the file every once in a while as you type it, so that if something happens to the computer or IDLE crashes, you won't lose everything you have typed.

Sample Run

H A N G M A N


    +------+
    |      |
    |      |
           |
           |
           |
           |
           |
           |
           |
           |
==============

Missed letters:
_ _ _ _ _
Guess a letter.
e


    +------+
    |      |
    |      |
           |
           |
           |
           |
           |
           |
           |
           |
==============

Missed letters:
_ _ _ e _
Guess a letter.
a


    +------+
    |      |
    |      |
    O      |
           |
           |
           |
           |
           |
           |
           |
==============

Missed letters: a
_ _ _ e _
Guess a letter.
u


    +------+
    |      |
    |      |
    O      |
    |      |
    |      |
    |      |
           |
           |
==============

Missed letters: a u
_ _ _ e _
Guess a letter.
r


    +------+
    |      |
    |      |
    O      |
    |      |
    |      |
    |      |
           |
           |
==============

Missed letters: a u
_ _ _ e r
Guess a letter.
i



    +------+
    |      |
    |      |
    O      |
   /|      |
  / |      |
    |      |
           |
           |
           |
           |
==============

Missed letters: a u i
_ _ _ e r
Guess a letter.
o



    +------+
    |      |
    |      |
    O      |
   /|      |
  / |      |
    |      |
           |
           |
           |
           |
==============

Missed letters: a u i
o _ _ e r
Guess a letter.
t
You have won!
Do you want to play again? (yes or no)
no

Source Code

hangman.py
  1. import random
  2. HANGMANPICS = ['''
  3.     +------+
  4.     |      |
  5.     |      |
  6.            |
  7.            |
  8.            |
  9.            |
  10.            |
  11.            |
  12.            |
  13.            |
  14. ==============''', '''
  15.     +------+
  16.     |      |
  17.     |      |
  18.     O      |
  19.            |
  20.            |
  21.            |
  22.            |
  23.            |
  24.            |
  25.            |
  26. ==============''', '''
  27.     +------+
  28.     |      |
  29.     |      |
  30.     O      |
  31.     |      |
  32.     |      |
  33.     |      |
  34.            |
  35.            |
  36. ==============''', '''
  37.     +------+
  38.     |      |
  39.     |      |
  40.     O      |
  41.    /|      |
  42.   / |      |
  43.     |      |
  44.            |
  45.            |
  46.            |
  47.            |
  48. ==============''', '''
  49.     +------+
  50.     |      |
  51.     |      |
  52.     O      |
  53.    /|\     |
  54.   / | \    |
  55.     |      |
  56.            |
  57.            |
  58.            |
  59.            |
  60. ==============''', '''
  61.     +------+
  62.     |      |
  63.     |      |
  64.     O      |
  65.    /|\     |
  66.   / | \    |
  67.     |      |
  68.    /       |
  69.   /        |
  70.            |
  71.            |
  72. ==============''', '''
  73.     +------+
  74.     |      |
  75.     |      |
  76.     O      |
  77.    /|\     |
  78.   / | \    |
  79.     |      |
  80.    / \     |
  81.   /   \    |
  82.            |
  83.            |
  84. ==============''']
  85. 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 yak zebra'.split()
  86. def getRandomWord(wordList):
  87.     # This function returns a random string from the passed list of strings.
  88.     wordIndex = random.randint(0, len(wordList) - 1)
  89.     return wordList[wordIndex]
  90. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  91.     print HANGMANPICS[len(missedLetters)]
  92.     print
  93.     print 'Missed letters:',
  94.     for letter in missedLetters:
  95.         print letter,
  96.     print
  97.     blanks = '_' * len(secretWord)
  98.     for i in range(len(secretWord)): # replace blanks with correctly guessed letters
  99.         if secretWord[i] in correctLetters:
  100.             blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
  101.     for letter in blanks: # show the secret word with spaces in between each letter
  102.         print letter,
  103.     print
  104. def getGuess(alreadyGuessed):
  105.     # Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.
  106.     while True:
  107.         print 'Guess a letter.'
  108.         guess = raw_input()
  109.         guess = guess.lower()
  110.         if len(guess) != 1:
  111.             print 'Please enter a single letter.'
  112.         elif guess in alreadyGuessed:
  113.             print 'You have already guessed that letter. Choose again.'
  114.         elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  115.             print 'Please enter a LETTER.'
  116.         else:
  117.             return guess
  118. def playAgain():
  119.     # This function returns True if the player wants to play again, otherwise it returns False.
  120.     print 'Do you want to play again? (yes or no)'
  121.     return raw_input().lower().startswith('y')
  122. print 'H A N G M A N'
  123. missedLetters = ''
  124. correctLetters = ''
  125. secretWord = getRandomWord(words)
  126. gameIsDone = False
  127. while True:
  128.     displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  129.     # Let the player type in a letter.
  130.     guess = getGuess(missedLetters + correctLetters)
  131.     if guess in secretWord:
  132.         correctLetters = correctLetters + guess
  133.         # Check if the player has won
  134.         foundAllLetters = True
  135.         for i in range(len(secretWord)):
  136.             if secretWord[i] not in correctLetters:
  137.                 foundAllLetters = False
  138.                 break
  139.         if foundAllLetters:
  140.             print 'You have won!'
  141.             gameIsDone = True
  142.     else:
  143.         missedLetters = missedLetters + guess
  144.         # Check if player has guessed too many times and lost
  145.         if len(missedLetters) == len(HANGMANPICS) - 1:
  146.             displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  147.             print 'You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"'
  148.             gameIsDone = True
  149.     # Ask the player if they want to play again (but only if the game is done).
  150.     if gameIsDone:
  151.         if playAgain():
  152.             missedLetters = ''
  153.             correctLetters = ''
  154.             gameIsDone = False
  155.             secretWord = getRandomWord(words)
  156.         else:
  157.             break

After typing in the source code (don't forget to save!) you can run this game by pressing F5. If any errors come up, be sure you typed the source code in exactly as it appears here. Remember that the indentation is important, and that lines will have zero, four, eight, or even twelve spaces in front of them.

Designing the Program

This game is a bit more complicated, so it will help if we take a moment to think about how we will put the program together. We will create a flow chart (like the flow chart at the end of the Dragon World chapter) to think about what this program will do. Of course, we don't have to write out a flow chart. We could just start writing code. But many times when we are writing code, we will think of new things to add or other events in the program that we didn't think of. We may have to end up changing the code we have already written, or deleting a lot of the code. That would be a waste of effort. We can save a lot of time if we think about the program before writing it.

This flow chart is provided as an example for what flow charts look like and how to make them. Because you only have to copy the source code from this book, you don't need to draw a flow chart before writing code. But when you make your own games, a flow chart can be very handy.

This flow chart will also help you learn how to design games yourself, instead of just copying the source code from this book. Your flow chart doesn't have to look exactly like this one. You may have extra boxes or fewer boxes. But as long as it you understand the flow chart you made, it will be helpful when you start coding.

First we'll start with a flow chart that only has a "Start" and an "End" box:

Now let's think about what happens when the play Hangman. Well, there is a secret word (the computer will think of this) and then the other person guesses letters (the player will do this) so let's add boxes for those:

The game doesn't end after the player guesses a letter. The game should check if the letter is in the secret word or not. The letter either will be or won't be, so we should put two new boxes in:

If the letter was in the secret word, we should also check if the player has won. And if the letter was not in the secret word, another body part gets added and the player might have lost. We can add boxes for those cases too. We don't need an arrow from the "Letter is in secret word." box to the "Player has run out of body parts and loses." box because if you think about it, you cannot possibly lose as long as you are guessing correct letters. Also, you cannot possibly win if you are guessing wrong letters. That is why we don't have those arrows. The flow chart now looks like:

After the player has won or lost, we will ask the player if they want to play again with a new secret word. If the player doesn't want to play again, the program will terminate. Otherwise, we will think of a new secret word.

This flow chart looks like it is finished, but is there something we are forgetting? Oh yes! The player doesn't guess a letter just once. The player will have to keep guessing letters over and over until they win or lose. We should draw two new arrows so the flow chart shows this.

What else are we forgetting? What if when the player guesses a letter, they guess a letter they have guessed before. The player should not win or lose in this case, but should be allowed to guess a different letter instead:

Wait a second. How can the player figure out how well or how bad they're doing in this game? We need to remember to show them the hangman board and also the secret word (with the unguessed letters blanked out). Then the player will be able to see how many body parts of the hangman there are, or how much of the secret word they have guessed so far. This should happen each time the player guesses. We can add this box in between the "Come up with a secret word." box and the "Ask player to guess a letter." box:

That looks good! We can always look at this flow chart while we are coding to remind ourselves of everything we want this program to do. The flow chart is kind of like a cake recipe or blueprints for a house. We could just start baking a cake or building a house, but without the plans we may forget to do a step. You won't really need this flow chart because you will just copy the source code given here. But when you design your own games, a flow chart can help you remember everything you need to code.

Code Explanation

  1. import random

The Hangman program is going to randomly select a secret word from a list of secret words. This means we will need the random module imported.

  1. HANGMANPICS = ['''
  2.     +------+
  3.     |      |
  4.     |      |
  5.            |
  6.            |
  7.            |
  8.            |
  9.            |
  10.            |
  11.            |
  12.            |
  13. ==============''', '''

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

This "line" of code a simple variable assignment, but it actually stretches over several real lines in the source code. The actual "line" doesn't end until line 96. To help you understand what this code means, you should learn about multi-line strings and lists:

Multi-line Strings

Ordinarily when you write strings in your source code, the string has to be on one line. However, if you use three single-quotes instead of one single-quote to begin and end the string, the string can be on several lines:

If we didn't have multi-line strings, we would have to use the \n escape character to represent the new lines. But that can make the string hard to read in the source code:

Notice that a multi-line string does not have to keep the same indentation to remain in the same block. Within the multi-line string, Python ignores the indentation rules it normally has for the where blocks end.

def writeLetter():
    # inside the def-block
    print '''Dear Alice,
How are you? Write back to me soon.

Sincerely,
  Bob''' # end of the multi-line string and print statement
    print 'P.S. I miss you.' # still inside the def-block

writeLetter() # This is the first line outside the def-block.

Constant Variables

You may have noticed that HANGMANPICS's name is in all caps. This is a programming convention for constant variables. Constants are variables whose values do not change throughout the program. Although we can change HANGMANPICS just like any other variable, the all-caps reminds the programmer to not write code that does so.

Constant variables are helpful for providing descriptions for special values. Since the multi-string value never changes, there is no reason we couldn't copy this multi-line string each time we needed that value. The HANGMANPICS variable never varies. But it is much shorter to type HANGMANPICS than it is to type that large multi-line string.

Also, there are cases where typing the value by itself may not be obvious. If we set a variable eggs = 72, we may forget why we were setting that variable to the integer 72. But if we define a constant variable DOZEN = 12, then we could set eggs = DOZEN * 6 and by just looking at the code know that the eggs variable was set to six dozen.

Lists

I will now tell you about a new data type called a list. A list can contain several values of different data types in it. Try typing this into the shell: ['apples', 'oranges', 'HELLO WORLD']. This is a list value that contains three string values. Just like any other value, you can store this list in a variable. Try typing spam = ['apples', 'oranges', 'HELLO WORLD'], and then type spam to view the contents of spam.

Lists are a good way to store several different values into one variable. Try typing: animals = ['aardvark', 'anteater', 'antelope', 'albert'] to store various strings into the variable animals. The square brackets can be used to get an individual item from a list. Try typing animals[0], or animals[1], or animals[2], or animals[3] into the shell to see what they evaluate to.

The number between the square brackets is the index. In Python, the first index is the number 0 instead of the number 1. So the first item in the list is at index 0, the second item is at index 1, the third item is at index 2, and so on.

Lists are very good when we have to store lots and lots of values, but we don't want variables for each one. Otherwise we would have something like this:

This makes working with all the strings as a group very hard, especially if you have hundreds or thousands (or even millions) of different strings that you want stored in a list.

Using the square brackets, you can treat items in the list just like any other value. Try typing animals[0] + animals[2] into the shell:

Because animals[0] evaluates to the string 'aardvark' and animals[2] evaluates to the string 'antelope', then the expression animals[0] + animals[2] is the same as 'aardvark' + 'antelope'. This string concatenation evaluates to 'aardvarkantelope'.

What happens if we enter an index that is larger than the list's largest index? Try typing animals[4] or animals[99] into the shell:

If you try accessing an index that is too large, you will get an index error.

Changing the Values of List Items with Index Assignment

You can also use the square brackets to change the value of an item in a list. Try typing animals[1] = 'ANTEATER', then type animals to view the list.

List Concatenation

You can join lists together into one list with the + operator, just like you can join strings. When joining lists, this is known as list concatenation. Try typing [1, 2, 3, 4] + ['apples', 'oranges'] + ['Alice', 'Bob'] into the shell:

The in Operator

The in operator makes it easy to see if a value is inside a list or not. Expressions that use the in operator return a boolean value: True if the value is in the list and False if the value is not in the list. Try typing 'antelope' in animals into the shell:

The expression 'antelope' in animals returns True because the string 'antelope' can be found in the list, animals. (It is located at index 2.)

But if we type the expression 'ant' in animals, this will return False because the string 'ant' does not exist in the list. We can try the expression 'ant' in ['beetle', 'wasp', 'ant'], and see that it will return True.

The in operator also works for strings as well as lists. You can check if one string exists in another the same way you can check if a value exists in a list. Try typing 'hello' in 'Alice said hello to Bob.' into the shell. This expression will evaluate to True.

Removing Items from Lists with del Statements

You can remove items from a list with a del statement. Try creating a list of numbers by typing: spam = [2, 4, 6, 8, 10] and then del spam[1]. Type spam to view the list's contents:

Notice that when you deleted the item at index 1, the item that used to be at index 2 became the new index 1. The item that used to be at index 3 moved to be the new index 2. Everything above the item that we deleted moved down one index. We can type del spam[1] again and again to keep deleting items from the list:

Lists of Lists

Lists are a data type that can contain other values as items in the list. These values can also be other lists. Let's say you have a list of groceries, a list of chores, and a list of your favorite pies. You can put all three of these lists into another list. Try typing this into the shell:


groceries = ['eggs', 'milk', 'soup', 'apples', 'bread']
chores = ['clean', 'mow the lawn', 'go grocery shopping']
favoritePies = ['apple', 'frumbleberry']
listOfLists = [groceries, chores, favoritePies]
listOfLists

You could also type the following and get the same values for all four variables:


listOfLists = [['eggs', 'milk', 'soup', 'apples', 'bread'], ['clean', 'mow the lawn', 'go grocery shopping'], ['apple', 'frumbleberry']]
groceries = listOfLists[0]
chores = listOfLists[1]
favoritePies = listOfLists[2]
listOfLists

To get an item inside the list of lists, you would use two sets of square brackets like this: listOfLists[1][2] which would evaluate to the string 'go grocery shopping'. This is because listOfLists[1] evaluates to the list ['clean', 'mow the lawn', 'go grocery shopping'][2]. That finally evaluates to 'go grocery shopping'.

A list that contains a list (or a list that contains a list that contains a list, and so on) is also called a matrix. (The plural of "matrix" is "matrices".)

Here is another example of a list of lists, along with some of the indexes that point to the items in the list of lists named x. The red arrows point to indexes of the inner lists themselves. The image is also flipped on its side to make it easier to read:

Code Explanation continued...

  1. HANGMANPICS = ['''
  2.     +------+
  3.     |      |
  4.     |      |
  5.            |
  6.            |
  7.            |
  8.            |
  9.            |
  10.            |
  11.            |
  12.            |
  13. ==============''', '''

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

If you look from line 3 to line 96 in the code, you will see that the value we are assigning to the 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.

  1. 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 yak 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 associated with a certain value. They are associated much in the same way that the randint() function is associated with the random module. Integers do not have any methods, but strings and lists do. To call a method, put a period and the method name with parentheses (and any arguments) after the value. For example, the string data type has a lower() method. You cannot just call the lower() by itself, but attach it with to a string. Try typing 'Hello world!'.lower() into the shell:

The lower() method returns the lowercase version of the string it is associated with. There is also an upper() method for strings. Try tying 'Hello world'.upper() into the shell:

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 the method on that variable. Look at this example:

The list data type also has methods. The reverse() method will reverse the items in the list. Try typing spam = [1, 2, 3, 4, 5, 6, 'meow', 'woof'] and then spam.reverse() (to reverse the list), and then 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

Code Explanation continued...

  1. 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 yak 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. 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:

  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]

Starting on line 100, we define a new functon 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.

The len() Function

The len() function takes a list as a parameter and returns the integer of how many items are in a list. Try typing len(animals) into the shell:

The integer value returned by len() is like any other integer value:

The square brackets by themselves is also a list value known as the empty list. If you pass the empty list to the len() function, it returns the integer 0, because there is nothing in that list:

Code Explanation continued...

  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]

When we call getRandomWord(), we will pass a list of strings as the argument to the parameter. On line 102, we will store a random index of this list in the wordIndex variable. We do this by calling randint() with two arguments. Remember that arguments in a function call are separated by commas, so the first argument is 0 and the second argument is len(wordList) - 1. The second argument is an expression that is first evaluated. len(wordList) will return the integer size of the list passed to getRandomWord(), minus one.

What this means is that if we passed ['apple', 'orange', grape'] as an argument to getRandomWord(), then len(wordList) would return the integer 3 and the expression would evaluate to 3 - 1. That would make the second argument to the randint() function call the integer 2.

That means that wordIndex would contain the return value of randint(0, 2), which means wordIndex would equal 0, 1, or 2. On line 103, we would return the element in wordList at the integer index stored in wordIndex.

Let's pretend we did send ['apple', 'orange', grape'] as the argument to getRandomWord() and randint(0, 2) returned the integer 2. That would mean that line 103 would become return wordList[2], which would evaluate to return 'grape'. In that case, the return value of our call to getRandomWord() would be the string 'grape'.

But remember, we can pass any list of strings to getRandomWord(). I hope this shows how a call to getRandomWord() will return a random string from the list of strings you passed the function call as an argument. This function will be very useful to our Hangman game when we call it.

  1. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  2.     print HANGMANPICS[len(missedLetters)]
  3.     print

This code defines a new function named displayBoard(). This function has four parameters. This function will implement the code for the "Show the board and blanks to the player" box in our flow chart. Here is what each parameter means:

The first print statement will display the board. HANGMANPICS will be a list of strings for each possible board. HANGMANPICS[0] shows an empty gallows, HANGMANPICS[1] shows the head (this happens when the player misses one letter), HANGMANPICS[2] shows a head and body (this happens when the player misses two letters), and so on until HANGMANPICS[6] when the full hangman is shown and the player loses.

We can tell how many missed guesses the player has made because that will be the number of letters in missedLetters. The return value of len(missedLetters) will be an integer of how many letters are in the string. We can use this integer as the index to the HANGMANPICS list. So, if missedLetters is 'aetr' then len('aetr') will return 4 and we will display the string at HANGMANPICS[4]. This is what HANGMANPICS[len(missedLetters)] evaluates to. This line shows the proper hangman board to the player.

  1.     print 'Missed letters:',
  2.     for letter in missedLetters:
  3.         print letter,
  4.     print

Line 110 is a new type of loop, called a for-loop. (Kind of like a while-loop.) Line 111 is the body of the for-loop, which is just a single line. The Python range() function is often used with for-loops. I will explain both in the next section.

The range() Function

The range() function is easy. You can call it with one or two integer arguments. Called with one argument, range() will return a list of integers from 0 up to (but not including) the argument. Try typing range(10) into the shell:

It's very easy to generate huge lists with the range() function. Try typing in range(10000) into the shell:

The list is so huge, that it won't even all fit onto the screen. But we can save the list into the variable just like any other list by typing spam = range(10000)

If you pass two arguments to range(), the list of integers it returns is from the first argument up to (but not including) the second argument. Try typing range(10, 20) into the shell:

The range() is a very useful function, because we often use it in for loops (which are much like the while loops we have already seen).

for Loops

The for loop is very good at looping over a list of values, instead of the while which loops as long as a condition is true. A for statement begins with the for keyword, followed by a variable, followed by the in, followed by a sequence (such as a list or string) and then a colon. Each time through the loop (that is, on each iteration through the loop) the variable in the for statement takes on the value of the next item in the list.

For example, you just learned that the range() function will return a list of integers. We will use this list as the for statement's list. In the shell, type for i in range(10): and press Enter. Nothing will happen, but the shell will indent the cursor, because it is waiting for you to type in the for-block. Type print i and press Enter. Then, to tell the interactive shell you are done typing in the for-block, press Enter again to enter a blank line. The shell will then execute your for statement and block:

The for loop executes the code inside the for-block once for each item in the list. Each time it executes the code in the for-block, the variable i is assigned the next value of the next item in the list. If we used the for statement with the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] instead of range(10), it would have been the same since the range() function's return value is the same as that list:

Try typing this into the shell: for thing in ['cats', 'pasta', 'programming', 'spam']:, press Enter, then print 'I really like ' + thing, then Enter, and then Enter again to tell the shell to end the for-block. The output should look like this:

And remember, because strings are also a sequence data type, you can use them in for statements as well:

You can make a while loop that acts the same way as a for loop by adding extra code:

But using the for statement automatically does all this extra code for us and makes programming easier since we have less to type. Our Hangman game will use for loops so you can see how they are useful in real games.

One more thing about for loops, is that the for statement has the in keyword in it. When you type "in" in a for statement, Python does not treat it like the in operator (like you would use for 42 in [0, 42, 67]). The "in" in for statements is just used to separate the variable and the list it gets its values from. It is just a part of the for statement.

Code Explanation continued...

  1.     print 'Missed letters:',
  2.     for letter in missedLetters:
  3.         print letter,
  4.     print

This for-loop will display all the letters that the player has guessed that are not in the secret word. When you play Hangman on paper, you usually write down these letters off to the side so you know not to guess them again. The value of letter will be each letter in missedLetters on each iteration of the loop. Remember that a comma at the end of the print statement will make it print a space instead of a "newline" character, so all the missed letters will be on the same line.

If missedLetters was 'ajtw' then this for-loop would print a j t w.

So by this point we have shown the player the hangman board and the missed letters. Now we want to show the secret word, except we want blank lines for the letters. We can use the _ underscore character for this. But we should show the letters in the secret word that the player has guessed. We can first create a string with nothing but underscores, one for each letter in the secret word. Then we can replace the blanks for each letter in correctLetters. So if the secret word was 'otter' then the blanked out string would be '_____' (that's five _ characters in a string). If correctLetters was the string 'rt' then we would want to change the blanked string to '_tt_r'. Here is the code that does that:

  1.     blanks = '_' * len(word)
  2.     
  3.     for i in range(len(secretWord)): # replace blanks with correctly guessed letters
  4.         if word[i] in correctLetters:
  5.             blanks = blanks[:i] + word[i] + blanks[i+1:]

The blanks variable will be the string full of _ underscores. Line 116 uses string multiplication. Remember that the * operator can also be used on a string and an integer, so the expression 'hello' * 3 evaluates to 'hellohellohello'. This will make sure that blanks has the same number of underscores as secretWord has letters.

Then we use a for loop to go through each letter in secretWord, and replace the underscore with the actual letter if it exists in correctLetters. Line 120 looks odd, it seems that we are adding the values at certain indexes in the blanks and secretWord variables. But wait a second, blanks and secretWord are strings, not lists. And the len() function also only takes lists as parameters, not strings. But in Python, many of the things you can do to lists you can also do to strings:

Strings Act Like Lists

Surprise! Strings act a lot like lists. In fact, almost all of the things you can do on lists you can also do on strings. Just think of strings as "lists" of one-letter strings. So 'Hello' acts similar to ['H', 'e', 'l', 'l', 'o']. (They are still different values and have different data types though.) The square brackets can also pick out individual characters from a string just like it can pick out individual items from a list. In the interactive shell, type fizz = 'Hello world!' and then fizz[0]:

You can also find out how many characters are in a string with the len() function. Type in len(fizz) into the shell:

However, you cannot change a character in a string or remove a character with del statement. This is because a list is a mutable sequence and a string is an immutable sequence. A "sequence" is a series of things (like in real life, a dance sequence is a series of different dance steps done one after another). "Mutable" is another word for "changeable". The word "immutable" means it cannot be changed. The reason strings are immutable and lists are mutable has to do with how the Python interpreter is programmed, but it isn't important for us to know in order to make games. If we want to change a string, we can create a copy of the string with slices.

So remember, you can use index assignment or del with lists but not with strings.

List Slicing and Substrings

Slicing is like indexing with multiple indexes instead of just one. Instead of putting one index in between the square brackets, we put two indexes separated by a colon. To grab the first three items from our animals list with animals[0:3], which means "all items in animals from item 0 up to (but not including) item 2".

To grab items 2 and 3 from animals, use the slice animals[2:4]. Try typing it into the shell:

You can use slicing to get a part of a string (called a substring from a string. Try typing 'Hello world!'[3:8] into the shell:

So remember that on the right side of any list or string value (or a variable that contains a list or string value), you can put square brackets to extract a single or several items from the sequence. ("Sequence" refers to a group of data types that include strings and lists.)

Code Explanation continued...

  1.     for i in range(len(secretWord)): # replace blanks with correctly guessed letters
  2.         if secretWord[i] in correctLetters:
  3.             blanks = blanks[:i] + secretWord[i] + blanks[i+1:]

Let's pretend the value of secretWord is 'otter' and the value in correctLetters is 'tr'. Then len(secretWord) will return 5. Then range(len(secretWord)) becomes range(5), which in turn returns the list [0, 1, 2, 3, 4].

Because the value of i will take on each value in [0, 1, 2, 3, 4], then the for-loop code is equivalent to this:

    if secretWord[0] in correctLetters:
        blanks = blanks[:0] + secretWord[0] + blanks[1:]
    if secretWord[1] in correctLetters:
        blanks = blanks[:1] + secretWord[1] + blanks[2:]
    if secretWord[2] in correctLetters:
        blanks = blanks[:2] + secretWord[2] + blanks[3:]
    if secretWord[3] in correctLetters:
        blanks = blanks[:3] + secretWord[3] + blanks[4:]
    if secretWord[4] in correctLetters:
        blanks = blanks[:4] + secretWord[4] + blanks[5:]

(By the way, writing out the code like this instead of using a loop is called loop unrolling.)

If you are confused as to what the value of something like secretWord[0] or blanks[3:] is, then look at this picture. It shows the value of the secretWord and blanks variables, and the index for each letter in the string.

If we replace the list slices and the list indexes with the values that they represent, the unrolled loop code would be the same as this:

    if 'o' in 'tr':                # Condition is False, blanks == '_____'
        blanks = '' + 'o' + '____' # This line is skipped.
    if 't' in 'tr':                # Condition is True, blanks == '_____'
        blanks = '_' + 't' + '___' # This line is executed.
    if 't' in 'tr':                # Condition is True, blanks == '_t___'
        blanks = '_t' + 't' + '__' # This line is executed.
    if 'e' in 'tr':                # Condition is False, blanks == '_tt__'
        blanks = '_tt' + 'e' + '_' # This line is skipped.
    if 'r' in 'tr':                # Condition is True, blanks == '_tt__'
        blanks = '_tt_' + 'r' + '' # This line is executed.

    # blanks now has the value '_tt_r'

The above three boxes of code all do the same thing (at least, they do when secretWord is 'otter' and correctLetters is 'tr'. The first box is the actual code we have in our game. The second box shows code that does the same thing except without a for-loop. The third box is the same as the second box, except we have evaluated many of the expressions in the second box.

The next few lines of code display the new value of blanks with spaces in between each letter.

  1.     for letter in blanks: # show the secret word with spaces in between each letter
  2.         print letter,
  3.     print

This for loop will print out each character in the string blanks. Remember that by now, blanks may have some of its underscores converted to the letters in secretWord. The comma at the end of the print statement causes it to display a space instead of a newline character.

This is the end of the displayBoard() function.


  1. def getGuess(alreadyGuessed):
  2.     # Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.

The getGuess() function has a string called alreadyGuessed which contains the letters the player has already guessed, and will ask the player to guess a single letter. This single letter will be the return value for this function.


  1.     while True:
  2.         print 'Guess a letter.'
  3.         guess = raw_input()
  4.         guess = guess.lower()

We will use a while loop because we want to keep asking the player for a letter until they enter text that is a single letter they have not guessed previously. Notice that the condition for the while loop is simply the boolean value True. That means the only way execution will ever leave this loop is by executing a break statement (which leaves the loop) or a return statement (which leaves the entire function).

The code inside the loop asks the player to enter a letter, which is stored in the variable guess. If the player entered a capitalized letter, it will be converted to lowercase on line 129.

elif ("Else If") Statements

Take a look at the following code:

if catName == 'Fuzzball':
    print 'Your cat is fuzzy.'
else:
    print 'Your cat is not very fuzzy at all.'

This code seems rather simple. If the catName variable is equal to the string 'Fuzzball', then the if statement's condition is True and we tell the user that her cat is fuzzy. If catName is anything else, then we tell the user her cat is not fuzzy.

But what if we wanted something else besides "fuzzy" and "not fuzzy"? We could put another if and else statement inside the first else block like this:

if catName == 'Fuzzball':
    print 'Your cat is fuzzy.'
else:
    if catName == 'Spots'
        print 'Your cat is spotted.'
    else:
        print 'Your cat is neither fuzzy nor spotted.'

But if we wanted more things, then the code starts to have a lot of indentation:

if catName == 'Fuzzball':
    print 'Your cat is fuzzy.'
else:
    if catName == 'Spots'
        print 'Your cat is spotted.'
    else:
        if catName == 'FattyKitty'
            print 'Your cat is fat.'
        else:
            if catName == 'Puff'
                print 'Your cat is puffy.'
            else:
                print 'Your cat is neither fuzzy nor spotted nor fat nor puffy.'

Typing all those spaces means you have more chances of making a mistake with the indentation. So Python has the elif keyword. Using elif, the above code looks like this:

if catName == 'Fuzzball':
    print 'Your cat is fuzzy.'
elif catName == 'Spots'
    print 'Your cat is spotted.'
elif catName == 'FattyKitty'
    print 'Your cat is fat.'
elif catName == 'Puff'
    print 'Your cat is puffy.'
else:
    print 'Your cat is neither fuzzy nor spotted nor fat nor puffy.'

If the condition for the if statement is False, then the program will check the condition for the first elif statement (which is catName == 'Spots'. If that condition is False, then the program will check the condition of the next elif statement. If ALL of the conditions for the if and elif statements are False, then the code in the else block executes.

But if one of the elif conditions are True, the elif-block code is executed and then execution jumps down to the first line past the else-block. So only one of the blocks in this if-elif-else statement will be executed. You can also leave off the else-block if you don't need one, and just have an if-elif statement.

Code Explanation continued...

  1.         if len(guess) != 1:
  2.             print 'Please enter a single letter.'
  3.         elif guess in alreadyGuessed:
  4.             print 'You have already guessed that letter. Choose again.'
  5.         elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  6.             print 'Please enter a LETTER.'
  7.         else:
  8.             return guess

The guess variable contains the text the player typed in for their guess. We need to make sure they typed in a single lowercase letter. If they didn't, we should loop back and ask them again. The if statement's condition checks that the text is one and only letter. If it is not, then we execute the if-block code, and then execution jumps down past the else-block. But since there is no more code after this if-elif-else statement, execution loops back to line 126.

If the condition for the if statement is False, we check the elif statement's condition on line 132. This condition is True if the letter exists inside the alreadyGuessed variable (remember, this is a string that has every letter the player has already guessed). If this condition is True, then we display the error message to the player, and jump down past the else-block. But then we would be at the end of the while-block, so execution jumps back up to line 126.

If the condition for the if statement and the elif statement are both False, then we check the second elif statement's condition on line 134. If the player typed in a number or a funny character (making guess have a value like '5' or '!'), then guess would not exist in the string 'abcdefghijklmnopqrstuvwxyz'. If this is the case, the elif statement's condition is True

Unless these three conditions are all False, the player will keep looping and keep being asked for a letter. But when all three of the conditions are False, then the else-block's return statement will run and we will exit this loop and function.

  1. def playAgain():
  2.     # This function returns True if the player wants to play again, otherwise it returns False.
  3.     print 'Do you want to play again? (yes or no)'
  4.     return raw_input().lower().startswith('y')

The playAgain() function has just a print statement and a return statement. The return statement has an expression that looks complicated, but we can break it down. Once we evaluate this expression to a value, that value will be returned from this function.

raw_input().lower().startswith('y')

This expression doesn't have any operators, but it does have a function call and two method calls. The function call is raw_input() and the method calls are lower() and startswith('y'). Remember that method calls are function calls that are attached by a period to the value on their left. lower() is attached to the return value of raw_input().

raw_input() returns a string of the text that the user typed in. The point of the playAgain() function is to let the player type in yes or no to tell our program if they want to play another round of Hangman. If the player types in YES, then the return value of raw_input() is the string 'YES'. 'YES'.lower() returns the lowercase version of the attached string. So the return value of 'YES'.lower() is 'yes'.

But there's the second method call, startswith('y'). This function returns True if the associated string begins with the string parameter between the parentheses, and False if it doesn't. The return value of 'yes'.startswith('y') is True.

Now we have evaluated this expression! We can see that what this does is let the player type in a response, we lowercase the response, check if it begins with the letter "y", and then return True if it does and False if it doesn't. Whew!

(On a side note, there is also a endswith(someString) string method that will return True if the string ends with the string in someString and False if it doesn't.)

Here's a step by step look at how Python evaluates this expression if the user types in YES.

return raw_input().lower().startswith('y')
      
return 'YES'.lower().startswith('y')
      
return 'yes'.startswith('y')
      
return True

That's all the functions we are creating for this game! getRandomWord(wordList) will take a list of strings passed to it as a parameter, and return one string from it. That is how we will choose a word for the player to guess.

displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord) will show the current state of the board, including how much of the secret word the player has guessed so far and the wrong letters the player has guessed. This function needs four parameters passed to work correctly. HANGMANPICS is a list of strings that hold the ASCII art for each possible hangman board. correctLetters and missedLetters are strings made up of the letters that the player has guessed that are in and not in the secret word. And secretWord is the secret word the player is trying to guess. This function has no return value.

getGuess(alreadyGuessed) takes a string of letters the player has already guessed and will keep asking the player for a letter that is a letter that he hasn't already guessed. (That is, a letter that is not in alreadyGuessed. This function returns the string of the acceptable letter the player guessed.

playAgain() is a function that asks if the player wants to play another round of Hangman. This function returns True if the player does and False if the player doesn't.

We'll now start the code for the main part of the game, which will call the above functions as needed. Look back at our flow chart.

We need to write code that does everything in this flow chart, and does it in order. The main part of the code starts at line 145:

  1. print 'H A N G M A N'
  2. missedLetters = ''
  3. correctLetters = ''
  4. secretWord = getRandomWord(words)
  5. gameIsDone = False

Line 145 is the first actual line that executes in our game. (Everything previous was just function definitions and a very large variable assignment for HANGMANPICS.) We start with a blank string for missedLetters and correctLetters. Then we call getRandomWord(words), where words is a variable with the huge list of possible secret words we assigned on line 98. The return value of getRandomWord(words) is one of these words, and we save it to the secretWord variable. Then we also set a variable named gameIsDone to False, which we will set to True when we want to signal that the game is over.


  1. while True:
  2.     displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)

The while loop's condition is always True, so we will always loop forever until a break statement is run. We will execute a break statement when the game is over (either because the player won or the player lost).

Line 152 calls our displayBoard() function, passing it the list of hangman ASCII art pictures and the three variables we set on lines 146, 147, and 148. Program execution moves to the start of displayBoard() at line 105. Based on how many letters the player has correctly guessed or missed, this function displays the hangman board to the player.


  1.     # Let the player type in a letter.
  2.     guess = getGuess(missedLetters + correctLetters)

If you look at our flow chart, you see only one arrow going from the "Show the board and the blanks to the player." box to the "Ask a player to guess a letter." box. Since we have already written a function to get the guess from the player, let's call that function. Remember that the function needs all the letters in missedLetters and correctLetters combined, so we will pass as an argument a string that is a concatenation of both of those strings. This argument is needed by getGuess() because the function has code to check if the player types in a letter that they have already guessed.


  1.     if guess in secretWord:
  2.         correctLetters = correctLetters + guess

Now let's see if the single letter in the guess string exists in secretWord. If it does, then we should concatenate the letter in guess to the correctLetters string. Next we can check if we have guessed all of the letters and won.

  1.         # Check if the player has won
  2.         foundAllLetters = True
  3.         for i in range(len(secretWord)):
  4.             if secretWord[i] not in correctLetters:
  5.                 foundAllLetters = False
  6.                 break

How do we know if the player has guessed every single letter in the secret word? Well, correctLetters has each letter that the player correctly guessed and secretWord is the secret word itself. We can't just check if correctLetters == secretWord because consider this situation: if secretWord was the string 'otter' and correctLetters was the string 'tore', then the player has guessed each letter in the secret. The player simply guessed the letters out of order, but they still win. Even if they did guess the letters in order, correctLetters would be the string 'oter' because the player can't guess the letter t more than once. The expression 'otter' == 'oter' would evaluate to False even though the player won.

The only way we can be sure the player won is to go through each letter in the secret word and see if it exists in correctLetters. If, and only if, every single letter in secretWord exists in correctLetters will the player have won.

Note that this is different than checking if every letter in correctLetters is in secretWord. If correctLetters was the string 'ot' and secretWord was 'otter', it would be true that every letter in 'ot' is in 'otter', but that doesn't mean the player has guessed the secret word and won.

So how can we do this? We can loop through each letter in secretWord and if we find a letter that does not exist in correctLetters, we know that the player has not guessed all the letters. This is why we create a new variable named foundAllLetters and set it to the boolean value True. We start out assuming that we have found all the letters, but will change foundAllLetters to False when we find a letter in secretWord that is not in correctLetters.

The for loop will go through the numbers 0 up to (but not including) the length of the word. Remember that range(5) will evaluate to the list [0, 1, 2, 3, 4, 5]. So on line 162, the program executes all the code inside the for-block with the variable i will be set to 0, then 1, then 2, then 3, then 4, then 5.

We use range(len(secretWord)) so that i can be used to access each letter in the secret word. So if the first letter in secretWord (which is located at secretWord[0]) is not in correctLetters, we know we can set foundAllLetters to False. Also, because we don't have to check the rest of the letters in secretWord, we can just break out of this loop. Otherwise, we loop back to line 163 and check the next letter.

If foundAllLetters manages to survive every single letter without being turned to False, then it will keep the original True value we gave it. Either way, the value in foundAllLetters is accurate by the time we get past this for loop and run line 166.


  1.         if foundAllLetters:
  2.             print 'You have won!'
  3.             gameIsDone = True

This is a simple check to see if we found all the letters. If we have found every letter in the secret word, we should tell the player that they have won. We will also set the gameIsDone variable to True. We will check this variable to see if we should let the player guess again or if the player is done guessing.


  1.     else:

This is the start of the else-block. Remember, the code in this block will execute if the condition was False. But which condition? To find out, point your finger at the start of the else keyword and move it straight up. You will see that the else keyword's indentation is the same as the if keyword's indentation on line 157. So if the condition on line 157 was False, then we will run the code in this else-block. Otherwise, we skip down past the else-block to line 177.


  1.         missedLetters = missedLetters + guess

Because the player's guessed letter was wrong, we will add it to the missedLetters string. This is like what we did on line 158 when the player guessed correctly.


  1.         # Check if player has guessed too many times and lost
  2.         if len(missedLetters) == len(HANGMANPICS) - 1:
  3.             displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  4.             print 'You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"'
  5.             gameIsDone = True

Think about how we know when the player has guessed too many times. When you play Hangman on paper, this is when the drawing of the hangman is finished. We draw the hangman on the screen with print statements, based on how many letters are in missedLetters. Remember that each time the player guesses wrong, we add (or as a programmer would say, concatenate) the wrong letter to the string in missedLetters. So the length of missedLetters (or, in code, len(missedLetters)) can tell us the number of wrong guesses.

At what point does the player run out of guesses and lose? Well, the HANGMANPICS list has 7 pictures (really, they are ASCII art strings). So when len(missedLetters) equals 6, we know the player has lost because the hangman picture will be finished. (Remember that HANGMANPICS[0] is the first item in the list, and HANGMANPICS[6] is the last one. This is because the index of a list with 7 items goes from 0 to 6, not 1 to 7.)

So why do we have len(missedLetters) == len(HANGMANPICS) - 1 as the condition on line 173, instead of len(missedLetters) == 6? Pretend that we add another string to the HANGMANPICS list (maybe a picture of the full hangman with a tail, or a third mutant arm). Then the last picture in the list would be at HANGMANPICS[7]. So not only would we have to change the HANGMANPICS list with a new string, but we would also have to remember to change line 173 to len(missedLetters) == 7. This might not be a big deal for a
program like Hangman, but when you start writing larger programs you may have to change several different lines of code all over your program just to make a
change in the program's behavior. This way, if we want to make the game harder or easier, we just have to add or remove ASCII art strings to HANGMANPICS and change nothing else.

A second reason we user len(HANGMANPICS) - 1 is so that when we read the code in this program later, we know why this program behaves the way it does. If you wrote len(missedLetters) == 6 and then looked at the code two weeks later, you may wonder what is so special about the number 6. You may have forgotten that 6 is the last index in the HANGMANPICS list. Of course, you could write a comment to remind yourself, like:

  1.         if len(missedLetters) == 6: # 6 is the last index in the HANGMANPICS list

But it is easier to just use len(HANGMANPICS) - 1 instead.

So, when the length of the missedLetters string is equal to len(HANGMANPICS) - 1, we know the player has run out of guesses and has lost the game. We print a long string telling the user what the secret word was, and then set the gameIsDone value to the boolean value True. This is how we will tell ourselves that the game is done and we should start over.

Remember that when we have \n in a string, that represents the newline character.


  1.     # Ask the player if they want to play again (but only if the game is done).
  2.     if gameIsDone:
  3.         if playAgain():
  4.             missedLetters = ''
  5.             correctLetters = ''
  6.             gameIsDone = False
  7.             secretWord = getRandomWord(words)

If the player won or lost after guessing their letter, then our code would have set the gameIsDone variable to True. If this is the case, we should ask the player if they want to play again. We already wrote the playAgain() function to handle getting a yes or no from the player. This function returns a boolean value of True if the player wants to play another game of Hangman, and False if they've had enough.

If the player does want to play again, we will reset the values in missedLetters and correctLetters to blank strings, set gameIsDone to False, and then choose a new secret word by calling getRandomWord() again, passing it the list of possible secret words.

This way, when we loop back to the beginning of the loop (on line 151) the board will be back to the start (remember we decide which hangman picture to show based on the length of missedLetters, which we just set as the blank string) and the game will be just as the first time we entered the loop. The only difference is we will have a new secret word, because we programmed getRandomWord() to return a randomly chosen word each time we call it.

There is a small chance that the new secret word will be the same as the old secret word, but this is just a coincidence. Let's say you flipped a coin and it came up heads, and then you flipped the coin again and it also came up heads. Both coin flips were random, it was just a coincidence that they came up the same both times. Accordingly, you may get the same word return from getRandomWord() twice in a row, but this is just a coincidence.


  1.         else:
  2.             break

If the player typed in 'no' when asked if they wanted to play again, then they return value of the call to the playAgain() function would be False and the else-block would have executed. This else-block only has one line, a break statement. This causes the execution to jump to the end of the loop that was started on line 151. But because there is no more code after the loop, the program terminates.

And that's it!

This program was much bigger than the Dragon World program, but this program is also more sophisticated. It really helps to make a flow chart or small sketch to remember how you want everything to work. Take a look at the flow chart and try to find the lines of code that represent each block.

At this point, you can move on to the next chapter. But I suggest you keep reading on to find out about some ways we can improve our Hangman game.

After you have played Hangman a few times, you might think that six guesses aren't enough to get many of the words. We can easily give the player more guesses by adding more multi-line strings to the HANGMANPICS list. It's easy, just change the ] square bracket on line 96 to a ,''' comma and three quotes (see line 96 below). Then add the following:

  1. ==============''', '''
  2.     +------+
  3.     |      |
  4.     |      |
  5.    [O      |
  6.    /|\     |
  7.   / | \    |
  8.     |      |
  9.    / \     |
  10.   /   \    |
  11.            |
  12.            |
  13. ==============''', '''
  14.     +------+
  15.     |      |
  16.     |      |
  17.    [O]     |
  18.    /|\     |
  19.   / | \    |
  20.     |      |
  21.    / \     |
  22.   /   \    |
  23.            |
  24.            |
  25. ==============''']

We have added two new multi-line strings to the HANGMANPICS list, one with the hangman's left ear drawn, and the other with both ears drawn. Because our program will tell the player they have lost when the number of guesses is the same as the number of strings in HANGMANPICS (minus one), this is the only change we need to make.

We can also change the list of words by changing the words on line 98. Instead of animals, we could have colors, shapes, or fruits:

  1. words = 'red orange yellow green blue indigo violet white black brown'.split()
  1. words = 'square triangle rectangle circle ellipse rhombus trapazoid chevron pentagon hexagon septagon octogon'.split()
  1. words = 'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantalope mango strawberry tomato'.split()

Dictionaries

With some modification, we can change our code so that our Hangman game can use all of these words as separate sets. We can tell the player which set the secret word is from (like "animal", "color", "shape", or "fruit"). This way, the player isn't guessing animals all the time.

To make this change, we will introduce a new data type called a dictionary. A dictionary is a collection of other values much like a list, but instead of accessing the items in the dictionary with an integer index, you access them with an index of any data type (but most often strings).

Try typing the following into the shell:

stuff = {'hello':'Hello there, how are you?', 'chat':'How is the weather?', 'goodbye':'It was nice talking to you!'}

Those are curly braces { and }. On the keyboard they are on the same key as the square braces [ and ]. We use curly braces to type out a dictionary value in Python. The values in between them are key-value pairs. The keys are the things on the left of the colon and the values are on the right of the colon. You can access the values (which are like items in lists) in the dictionary by using the key (which are like indexes in lists). Try typing into the shell stuff['hello'] and stuff['chat'] and stuff['goodbye']:

You see, instead of putting an integer index in between the square brackets, you put a key string index. This will evaluate to the value for that key. You can get the size (that is, how many key-value pairs in the dictionary) with the len() function. Try typing len(stuff) into the shell:

The list version of this dictionary would have only the values, and look something like this:

listStuff = ['Hello there, how are you?', 'How is the weather?', 'It was nice talking to you!']

The list doesn't have any keys, like 'hello' and 'chat' and 'goodbye' in the dictionary. We have to use integer indexes 0, 1, and 2.

Dictionaries are different from lists because they are unordered. The first item in a list named listStuff would be listStuff[0]. But there is no "first" item in a dictionary, because dictionaries do not have any sort of order. Try typing this into the shell:

favorites1 = {'fruit':'apples', 'animal':'cats', 'number':42}
favorites2 = {'animal':'cats', 'number':42, 'fruit':'apples'}
favorites1 == favorites2

As you can see, the expression favorites1 == favorites2 evaluates to True because dictionaries are unordered, and they are considered to be the same if they have the same key-value pairs in them. Lists are ordered, so a list with the same values in them but in a different order are not the same. Try typing this into the shell:

listFavs1 = ['apples', 'cats', 42]
listFavs2 = ['cats', 42, 'apples']
listFavs1 == listFavs2

As you can see, the two lists listFavs1 and listFavs2 are not considered to be the same because order matters in lists.

You can also use integers as the keys for dictionaries. Dictionaries can have keys of any data type, not just strings. But remember, because 0 and '0' are different values, they will be different keys. Try typing this into the shell:

myDict = {'0':'a string', 0:'an integer'}
myDict[0]
myDict['0']

You might think that using a for loop is hard with dictionaries because they do not have integer indexes. But actually, it's easy. Try typing the following into the shell. (Here's a hint, in IDLE, you do not have to type spaces to start a new block. IDLE does it for you. To end the block, just insert a blank line by just hitting the Enter key. Or you could start a new file, type in this code, and then press F5 to run the program.)

favorites = {'fruit':'apples', 'animal':'cats', 'number':42}
for i in favorites:
    print i
for i in favorites:
    print favorites[i]

As you can see, if you just use a dictionary in a for loop, the variable i will take on the values of the dictionary's keys, not it's values. But if you have the dictionary and the key, you can get the value as we do above with favorites[i]. But remember that because dictionaries are unordered, you cannot predict which order the for loop will execute in. Above, we typed the 'animal' key as coming before the 'number' key, but the for loop printed out 'number' before 'animal'.

Dictionaries also have two useful methods, keys() and values(). These will return (ordered) lists of the key values and the value values, respectively. Try typing the following into the shell:

favorites = {'fruit':'apples', 'animal':'cats', 'number':42}
favorites.keys() favorites.values()

Using these methods to get a list of the keys and values that are in a dictionary can be very helpful.

Sets of Words for Hangman

So how can we use dictionaries in our game? First, let's change the list words into a dictionary whose keys are strings and values are lists of strings. (Remember that the string method split() evaluates to a list.


  1. words = {'Colors':'red orange yellow green blue indigo violet white black brown'.split(),
  2. 'Shapes':'square triangle rectangle circle ellipse rhombus trapazoid chevron pentagon hexagon septagon octogon'.split(),
  3. 'Fruits':'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantalope mango strawberry tomato'.split(),
  4. 'Animals':'bat bear beaver cat cougar crab deer dog donkey duck eagle fish frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat zebra'.split()}

This code is put across multiple lines in the file, even though the Python interpreter thinks of it as just one "line of code". (The line of code doesn't end until the final } curly brace.)

Now we will have to change our getRandomWord() function so that it chooses a random word from a dictionary of lists of strings, instead of from a list of strings. Here is what the function originally looked like:


  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]

Change the code in this function so that it looks like this:


  1. def getRandomWord(wordDict):
  2.     # This function returns a random string from the passed dictionary of lists of strings, and the key also.
  3.     # First, randomly select a key from the dictionary:
  4.     wordKey = random.choice(wordDict.keys())
  5.     # Second, randomly select a word from the key's list in the dictionary:
  6.     wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
  7.     return [wordDict[wordKey][wordIndex], wordKey]

Line 100 just changes the name of the parameter to something a little more descriptive. Now instead of choosing a random word from a list of strings, first we choose a random key from the dictionary and then we choose a random word from the key's list of strings. Line 104 calls a new function in the random module named choice(). The choice() function has one parameter, a list. The return value of choice() is an item randomly selected from this list each time it is called.

Remember that randint(a, b) will return a random integer between (and including) the two integers a and b and choice(a) returns a random item from the list a. Look at these two lines of code, and figure out why they do the exact same thing:

random.randint(0, 9)
random.choice(range(0, 10))

Line 103 (line 109 in the new code) has also been changed. Now instead of returning the string wordList[wordIndex], we are returning a list with two items. The first item is wordDict[wordKey][wordIndex]. The second item is wordKey. We return a list because we actually want the getRandomWord() to return two values, so putting those two values in a list and returning the list is the easiest way to do this.

wordDict[wordKey][wordIndex] may look kind of complicated, but it is just an expression you can evaluate one step at a time like anything else. First, imagine that wordKey had the value 'Fruits' (which was chosen on line 104) and wordIndex has the value 5 (chosen on line 107). Here is how wordDict[wordKey][wordIndex] would evaluate:

wordDict[wordKey][wordIndex]
    
wordDict['Fruits'][5]
    
['apple', 'orange', 'lemon', 'lime', 'pear', 'watermelon', 'grape', 'grapefruit', 'cherry', 'banana', 'cantalope', 'mango', 'strawberry', 'tomato'][5]
    
'watermelon'

In the above case, the first item in the list this function returns would be the string 'watermelon'.

There are just three more changes to make to our program. The first two are on the lines that we call the getRandomWord() function. The function is called on lines 148 and 184 in the original program:

  1. correctLetters = ''
  2. secretWord = getRandomWord(words)
  3. gameIsDone = False
...
  1.             gameIsDone = False
  2.             secretWord = getRandomWord(words)
  3.         else:

Because the getRandomWord() function now returns a list of two items instead of a string, secretWord will be assigned a list, not a string. We would then have to change the code as follows:

  1. correctLetters = ''
  2. secretWord = getRandomWord(words)
  3. secretKey = secretWord[1]
  4. secretWord = secretWord[0]
  5. gameIsDone = False
...
  1.             gameIsDone = False
  2.             secretWord = getRandomWord(words)
  3.             secretKey = secretWord[1]
  4.             secretWord = secretWord[0]
  5.         else:

With the above changes, secretWord is first a list of two items. Then we add a new variable named secretKey and set it to the second item in secretWord. Then we set secretWord itself to the first item in the secretWord list. That means that secretWord will then be a string.

But there is an easier way by doing a little trick with assignment statements. Try typing the following into the shell:

a, b, c = ['apples', 'cats', 42]
a
b
c

The trick is to put the same number of variables (delimited by commas) on the left side of the = sign as are in the list on the right side of the = sign. Python will automatically assign the first item's value in the list to the first variable, the second item's value to the second variable, and so on. But if you do not have the same number of variables on the left side as there are items in the list on the right side, the Python interpreter will give you an error.

So we should change our code in Hangman to use this trick, which will mean our program uses fewer lines of code.


  1. correctLetters = ''
  2. secretWord, secretKey = getRandomWord(words)
  3. gameIsDone = False
...
  1.             gameIsDone = False
  2.             secretWord, secretKey = getRandomWord(words)
  3.         else:

The last change we will make is to add a simple print statement to tell the player which set of words they are trying to guess. This way, when the player plays the game they will know if the secret word is an animal, color, shape, or fruit. Add this line of code after line 151. Here is the original code:


  1. while True:
  2.     displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)

Add the line so your program looks like this:


  1. while True:
  2.     print 'The secret word is in the set: ' + secretKey
  3.     displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)

Now we are done with our changes. Instead of just a single list of words, the secret word will be chosen from many different lists of words. We will also tell the player which set of words the secret word is from. Try playing this new version. You can easily change the words dictionary on line 98 to include more sets of words.