Hangman — Word Guessing Game
In this assignment, you'll build a classic Hangman game where the player tries to guess a secret word one letter at a time before running out of chances. This project uses all the same building blocks as Rock Paper Scissors, just combined in a slightly new way:
random.choice() to pick the secret wordClick below to download all the files you need to get started:
By completing this assignment, you'll be able to:
random.choice() to select randomly from a listfor loop+=break to exit a loop when a win condition is metWork through these steps in order. Write a little, run it, make sure it works, then move on. Don't try to write the whole program at once.
Use cheat_sheet.py when you get stuck on syntax — it has examples of every technique you need.
At the top of hangman.py (after the import), a word_list is already defined. Your
job is to use random.choice() to pick one word from that list and store it in
secret_word.
Then print secret_word temporarily so you can see it while testing. You'll remove that print
later.
After this step: run the file — you should see a random word printed each time.
Make sure guessed_letters, wrong_guesses, and max_wrong are all set
correctly (TODOs 2–4).
Then write the welcome message (TODO 5). Use len(secret_word) to tell the player how many
letters the word has.
After this step: run the file — you should see the welcome message and then the program stops.
This is the heart of the game. Inside the while loop, complete TODO 6:
display = ""for loop to go through each letter in secret_wordif/else:
letter is in guessed_letters → add letter + " ""_ "
Then print display so you can see it. Since guessed_letters starts empty, you
should see all underscores.
After this step: run the file — you should see something like _ _ _ _ _ _ for a 6-letter
word, repeating every loop iteration.
Important: The while loop currently has no way to exit — press
Ctrl+C to stop it while testing this step.
Complete TODOs 7 and 8:
Wrong guesses: 0 / 6)
"_" is not in display, all letters are
revealed — print a congratulations message and use break to end the loop.
Hint for TODO 8: if "_" not in display is true when every letter has been guessed. This
won't trigger yet since no letters are guessed — that's expected.
Complete TODOs 9, 10, and 11:
", ".join(guessed_letters) to format the list
nicely. See cheat_sheet.py section 7 for an example.
guess = input(...).lower() is already there.while loop. Inside it, print a message explaining the
problem, then ask for a new guess with input().
Test it: Run the game and try typing multiple characters, then try repeating a letter after
it's been added to guessed_letters. It should reject both and keep asking.
Complete TODOs 12 and 13:
guessed_letters using .append().guess is in secret_word. Print "Good guess!" if correct.
Otherwise add 1 to wrong_guesses and print how many guesses are left.
Test it: Play a full round. You should see the display update with correct letters, and the wrong guess count go up for incorrect ones.
Complete TODO 14:
After the while loop ends, check if wrong_guesses == max_wrong. If so, the player
ran out of guesses — print a game over message that reveals the secret word.
Test it: Play until you lose on purpose. The word should be revealed at the end.
Remove the temporary print(secret_word) you added in Step 1. Play through the game a few more
times to make sure both the win and loss endings work correctly.
Before you're done, make sure your game does all of these:
display isn't showing what you expect,
add print(display) right after you build it.
for letter in "python", letter will be "p", then "y",
then "t", and so on.
while loop must be indented.
Everything inside the for loop must be indented further.
Ctrl+C to stop an infinite loop. If your loop has no way to exit during
testing, this will break you out.
cheat_sheet.py when you get stuck on
syntax — that's exactly what it's for.
Here's what a complete game could look like when it runs:
Welcome to Hangman! The word has 7 letters. You have 6 guesses.
Word: _ _ _ _ _ _ _
Wrong guesses: 0 / 6
Letters guessed:
Guess a letter: p
Good guess!
Word: p _ _ _ _ _ _
Wrong guesses: 0 / 6
Letters guessed: p
Guess a letter: e
Nope! 5 guesses left.
Word: p _ _ _ _ _ _
Wrong guesses: 1 / 6
Letters guessed: p, e
Guess a letter: pp
Please enter a single letter.
Guess a letter: r
Good guess!
Word: p r _ _ r _ _
Wrong guesses: 1 / 6
Letters guessed: p, e, r
Guess a letter: o
Good guess!
Word: p r o _ r _ _
Wrong guesses: 1 / 6
Letters guessed: p, e, r, o
Guess a letter: g
Good guess!
Word: p r o g r _ _
Wrong guesses: 1 / 6
Letters guessed: p, e, r, o, g
Guess a letter: a
Good guess!
Word: p r o g r a _
Wrong guesses: 1 / 6
Letters guessed: p, e, r, o, g, a
Guess a letter: m
Good guess!
You got it! The word was: program
hangman.py — Your main game file
(start here)
cheat_sheet.py — Reference
snippets for for loops, lists, string building, and more