Date

Due: Wednesday, October 12, 11:00 AM

Write a program called hangman.py that will let the user play Hangman as shown in the examples below.

The user will guess letters until either:

  • They have correctly guessed all the letters in the word.
  • They have incorrectly guessed 6 times.

At each turn, display the player's progress by showing the word they're guessing in all capital letters, with all non-guessed characters replaced by dashes ("-").

Suggested Approach

Here's one possible way to approach this program:

  1. Prompt the user to enter a word
  2. Create a display string with the right number of hyphens (loop and add one for each letter in the target word).
  3. For each letter they guess,
    1. If the letter is not in the target word, count a miss
    2. Otherwise, make a new display string by
      1. loop through the target word's letters
        1. if the letter matches their guess, add it to the new display string
        2. if the letter does not match their guess, copy whatever was in that spot of the old display string over
      2. replace the old display string with the new one
    3. If they have too many misses, tell them they lose; if there are no - left in the display string, tell them they win.

The specific output format is shown in the examples below:

Enter a word: HANGMAN
[-------] You have 6 left, enter a letter: A
Correct!
[-A---A-] You have 6 left, enter a letter: B
Incorrect!
[-A---A-] You have 5 left, enter a letter: C
Incorrect!
[-A---A-] You have 4 left, enter a letter: D
Incorrect!
[-A---A-] You have 3 left, enter a letter: E
Incorrect!
[-A---A-] You have 2 left, enter a letter: F
Incorrect!
[-A---A-] You have 1 left, enter a letter: G
Correct!
[-A-G-A-] You have 1 left, enter a letter: K
You lose! The word was "HANGMAN"

Another example:

Enter a word: COMPUTER
[--------] You have 6 left, enter a letter: E
Correct!
[------E-] You have 6 left, enter a letter: B
Incorrect!
[------E-] You have 5 left, enter a letter: C
Correct!
[C-----E-] You have 5 left, enter a letter: A
Incorrect!
[C-----E-] You have 4 left, enter a letter: P
Correct!
[C--P--E-] You have 4 left, enter a letter: E
Correct!
[C--P--E-] You have 4 left, enter a letter: R
Correct!
[C--P--ER] You have 4 left, enter a letter: M
Correct!
[C-MP--ER] You have 4 left, enter a letter: A
Incorrect!
[C-MP--ER] You have 3 left, enter a letter: T
Correct!
[C-MP-TER] You have 3 left, enter a letter: O
Correct!
[COMP-TER] You have 3 left, enter a letter: U
You win! The word was "COMPUTER"

Another example:

Enter a word: Aa
[--] You have 6 left, enter a letter: a
You win! The word was "AA"

Notes
  • We will only grade you on words and guesses made up of letters in the alphabet.
  • Convert both the word and the guesses to upper case; word Aa and guess a should say You win! The word was "AA".
  • If the user guesses a letter twice, handle it the same as the first time. If it initially matched a letter in the string, it should still count as a match and not as a miss. If it was initially a miss, it will count as another miss. This is shown in the examples above.
  • You may assume that the user only types single-letter guesses.

Submission: Please submit one .py file named hangman.py to the POTD submission system at https://archimedes.cs.virginia.edu/cs1110/