# FIRST RUN this code block of imports import random # import random library from hangman import hangman_pics # import hangman pictures
Let's try recreating the game hangman!
If you haven't played before, the goal is to guess a word within 6 tries!
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.
# 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:
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).
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__
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!
# Test your function here
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
cat, and the player has correctly guessed
a, then the current word state should be
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
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:
letters_guessed is a string of the letters that have been guessed already.
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.
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.
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
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.
word = random.choice(words) play_hangman(word)
Only attempt these once you've finished the lab and have extra time.