In [ ]:
# FIRST RUN this code block of imports
import random                     # import random library
from hangman import hangman_pics  # import hangman pictures


# Hangman¶

Let's try recreating the game hangman!
If you haven't played before, the goal is to guess a word within 6 tries!

• The player is given the number of letters in the word as underscores _.
• Each turn, they guess a letter. If the letter is in the word, we will fill in all the places where that letter exists.
• The player gets 6 chances for incorrect guesses, after which they lose.
• Once they have guessed the entire word, they win.

## Choosing Words¶

First we need to give the program a list of possible words to choose from.
Feel free to add or take away words from this list. Then run the code block.

In [ ]:
# A list of possible words
# Feel free to add more or change these!
words = ['mochi', 'boba', 'dinosaur', 'clam', 'dog', 'balcony',
'tree', 'buildings', 'forest', 'signature', 'peak', 'projector',
'lightning', 'backpack', 'spaghetti', 'ravioli']


In order to choose a word, we can use the function random.choice() from the random library. If we pass a list to this function, it will return one of the items at random. For example, try running this code block several times:

In [ ]:
random.choice(words)


## Display the Board¶

We need to print the current state of the game.

hangman_pics is a list of strings representing different states of the hangman, from 0 (no incorrect guesses) to 6 (6 incorrect guesses).

The function display_board is passed the following parameters:

• missed_letters: a string of missed letters.
• word_state: a string of the current word state, with underscores and letters.

We want to print the current hangman state, the missed letters, and the current word state. For example, display_board('aiu', '_r__') should print something like:

     +---+
|   |
O   |
/|   |
|
|
=========
Missed letters: aiu
_r__
In [ ]:
def display_board(missed_letters, word_state):
# Print the current hangman state.
# HINT: Index into the list hangman_pics to get the string to print.

# Print the letters they've missed

# Print the current word state



Test your function by calling display_board('aiu', '_r__'). It should look like the example!

In [ ]:
# Test your function here


## Getting the Word State¶

We need to get the word state based on the word and the player's previous guesses.

Remember, the word state shows how many letters have currently been guessed correctly in the word.

For example, if the word is cat, and the player has correctly guessed a, then the current word state should be c_t.

In [ ]:
def get_word_state(word, correct_letters):
word_state = ''  # initialize word_state

# For every letter in the word...

# If the letter is in correct_letters (it has been correctly guessed)...

# Add the letter to word_state

# Otherwise...

# Add an underscore to word_state

# Return the word state



## Getting the Guess¶

We need to get and validate the player's guess. This function will loop until the player's guess is valid. A guess has the following requirements:

• Guess must have length 1.
• Guess must be a letter in the alphabet.
• Guess cannot have already been guessed.

The argument letters_guessed is a string of the letters that have been guessed already.

In [ ]:
def get_guess(letters_guessed):
valid_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # valid characters
valid_guess = False                                                     # assume guess is not valid

# While the guess is not valid...

# Get input from the player (this is written for you)
guess = raw_input('Guess a letter: ')

# If guess does not have length 1, tell the player

# Otherwise, guess is not a valid letter, tell the player. (Hint: elif)

# Otherwise, guess has already been guessed, tell the player. (Hint: elif)
# Remember, letters_guessed is a string of the letters that have been guessed already.

# Otherwise,(Hint: else) set valid_guess to True

# Return the guess
return guess



Try running the code block below a few times. The letters a, b, c, d, e, f, and g have already been guessed. Make sure your validation and feedback works.

In [ ]:
get_guess('abcdefg')


## The Game¶

Now let's put everything together! We want to continue playing until we have won or lost, so we can use a loop! Think about which type of loop would work best here.

In [ ]:
def play_hangman(word):
game_over = False             # keeps track of if the game is over
correct_letters = ''          # string of correctly guessed letters
missed_letters = ''           # string of incorrectly guessed letters
word_state = '_' * len(word)  # the word state (start with all underscores)

# While the game is not over...

# Display the current board (call the display_board function!)

# Find all letters_guessed (Hint: Use concatenation between correct and missed letters)

# Get and save a guess from the user (call the get_guess function!)

# If the guess is correct (if the guess is in the word)...

# Print correct guess message

# Update correct_letters (add the guess)

# Update word_state (call the get_word_state function)

# If they have won the game (there will be no more underscores in word_state)...

# Print the board (call display_board!)

# Print winning message

# Set game_over to True

# Otherwise, if the guess is incorrect...

# Print incorrect guess message

# Update missed_letters

# If they have lost the game (6 or more missed letters)

# Print the board (call display_board!)

# Let the player know what the correct word was!

# Print losing message

# Set game_over to True



## Playing the Game¶

If everything works, you should now be able to play the game by running the code block below. Play the game a few times and test to see if there are any bugs.

In [ ]:
word = random.choice(words)
play_hangman(word)


## More Hangman Ideas!¶

Only attempt these once you've finished the lab and have extra time.

• Add more turns to the hangman (ie. shoes, ears, etc.).
• Create categories from which the word is chosen from (ie. colors, shapes, animals, fruits) (hint: dictionaries)
• Create different difficulty levels.
• Create a counter to keep track of the number of games that the user has won and lost (and calculate the score at the end).