Week 2 Extra Credit Assignment

Hangman — Word Guessing Game

📝 Overview

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:

Expected Time: 3-4 hours
Difficulty: Easy to Medium
Recommended: Week 2 (Python Fundamentals)

📥 Download Project Files

Click below to download all the files you need to get started:

🎯 Learning Objectives

By completing this assignment, you'll be able to:

  1. Use random.choice() to select randomly from a list
  2. Loop over a string character by character with a for loop
  3. Build a string dynamically inside a loop using +=
  4. Track state across multiple rounds using a list and a counter variable
  5. Use break to exit a loop when a win condition is met
  6. Validate user input and re-prompt until a valid answer is given

🪜 Step-by-Step Guide

Work 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.

Step 1 — Pick a Secret Word

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.

Step 2 — Set Up Your Variables and Welcome Message

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.

Step 3 — Build the Display String

This is the heart of the game. Inside the while loop, complete TODO 6:

  1. Start with display = ""
  2. Use a for loop to go through each letter in secret_word
  3. Inside the loop, use an if/else:
    • If letter is in guessed_letters → add letter + " "
    • Otherwise → add "_ "

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.

Step 4 — Add Round Info and Win Detection

Complete TODOs 7 and 8:

  • TODO 7: Print the display and the current wrong guess count (e.g., Wrong guesses: 0 / 6)
  • TODO 8: Check if the player has won. If "_" 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.

Step 5 — Get and Validate the Player's Guess

Complete TODOs 9, 10, and 11:

  • TODO 9: Print the letters already guessed. Use ", ".join(guessed_letters) to format the list nicely. See cheat_sheet.py section 7 for an example.
  • TODO 10: Ask for input — the first guess = input(...).lower() is already there.
  • TODO 11: Complete the validation 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.

Step 6 — Record the Guess and Check the Word

Complete TODOs 12 and 13:

  • TODO 12: Add the guess to guessed_letters using .append().
  • TODO 13: Check if 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.

Step 7 — Handle Game Over

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.

Step 8 — Clean Up

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.

📋 Requirements Checklist

Before you're done, make sure your game does all of these:

  1. Picks a random secret word from the word list at the start
  2. Tells the player how many letters the word has
  3. Each round: shows current progress (letters + blanks), wrong guess count, and letters already tried
  4. Accepts input case-insensitively ("A" and "a" both work)
  5. Rejects guesses that are more than one character or already guessed
  6. Correctly reveals letters in the display as the player guesses them
  7. Ends with a win message if all letters are revealed before 6 wrong guesses
  8. Ends with a loss message that reveals the word if the player reaches 6 wrong guesses

💡 Tips for Success

  1. Run after every step. You'll catch bugs immediately instead of having to hunt through 50 lines of code.
  2. Print variables to understand them. If display isn't showing what you expect, add print(display) right after you build it.
  3. The for loop iterates one character at a time. If you do for letter in "python", letter will be "p", then "y", then "t", and so on.
  4. Check your indentation. Everything inside the while loop must be indented. Everything inside the for loop must be indented further.
  5. Use Ctrl+C to stop an infinite loop. If your loop has no way to exit during testing, this will break you out.
  6. The cheat sheet is not cheating. Open cheat_sheet.py when you get stuck on syntax — that's exactly what it's for.

🎮 Example Session

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
        

📁 Files in Your Assignment