Python Bibliotheca
Python resources for teachers and students.

Hangman

Description

Hangman is a popular word guessing game where the player attempts to construct a missing word by guessing one letter at a time. After a certain number of incorrect guesses, the game ends and the player loses. The game also ends if the player correctly identifies all the letters of the missing word.

Input

Your program will prompt the user for letter guesses until the word is correctly guessed or the player has exceeded the maximum number of guesses. User input should be checked to make sure it's valid. A large file full of words is available for you to use in your program. You will certainly need to write some code to filter out the words of a certain length. Perhaps you could prompt the player for the length of the word they would like to guess and adjust the number of allowable wrong guesses accordingly.

Output

Your program should print a list of letters that have been guessed as well as display the correctly guessed letters in the word. A graphical representation of the hanging man (kinda gross when you think about it) is optional.

Sample Run

Welcome to hangman. You get seven chances to guess the mystery word.

_ _ _ _ _ _

Pick a letter --> e

Guessed letters: E

 O

_ _ _ _ _ _

Pick a letter --> a

Guessed letters: E A

 O
 |

_ _ _ _ _ _

Pick a letter --> e
Sorry, you already guessed 'E'
Pick a letter --> i

Guessed letters: E A I

 O
\|

_ _ _ _ _ _

Pick a letter --> o

Guessed letters: E A I O

 O
\|

_ O _ _ _ _

Pick a letter --> u

Guessed letters: E A I O U

 O
\|/

_ O _ _ _ _

Pick a letter --> y

Guessed letters: E A I O U Y

 O
\|/

_ O _ _ _ Y

Pick a letter --> 4
'4' is not a valid letter
Pick a letter --> xyz
'XYZ' has more than one letter.
Pick a letter --> l

Guessed letters: E A I O U Y L

 O
\|/

_ O _ _ L Y

Pick a letter --> s

Guessed letters: E A I O U Y L S

 O
\|/
 |

_ O _ _ L Y

Pick a letter --> r
 
Guessed letters: E A I O U Y L S R
 
 O
\|/
 |
/
 
_ O _ _ L Y
 
Pick a letter --> k
 
Guessed letters: E A I O U Y L S R K
 
 O
\|/
 |
/ \
 
_ O _ _ L Y
 
So sorry. You struck out.
The mystery word was 'COMPLY.'

[ Copyright 2003, Tim Wilson ]

Comments, questions, or suggestions? Email the webmaster