For those that don't know, Hangman is a word guessing game traditionally played with pen and paper. A word is secretly picked, and the only thing known about it is the number of letters it has. The player than guesses a letter and, if it it's in the word, it's position is revealed. Incorrect guesses count against the player. If you guess the word you win, make too many wrong guesses and it's game over.
Right, now we all know what the game entails, let's see how this might look when played in Python.
Word: ---
Wrong guesses:
Guess a letter > a
Word: -a-
Wrong guesses:
Guess a letter > g
Word: -a-
Wrong guesses: g
Guess a letter > t
Word: -at
Wrong guesses: g
Guess a letter > c
Word: cat
Well done! You guessed the word with 1 wrong guesses.
Getting started
Let's start a new Python program to make this game. We'll need a word to guess and a way to keep track of correct and incorrect guesses.
word = "something"
guessed_letters = []
incorrect_letters = []
What's with the square brackets? That's Python speak for a new list
. A list is a variable that can store more than one value. In this case we are going to store letters in the lists, but Python will let you put anything in there.
Our game is essentially going to be a loop that shows the player the parts of the word guessed so far, and then asks them for another guess. We can use a while
loop to implement this. But what should our condition be for ending the loop? Well there are two reasons we would want to end the game:
- The player has guessed all the correct letters.
- The player has made too many wrong guesses.
The first condition is met when the number of letters in the guessed_letters
list is the same as the number of letters in the word. So our loop needs to run while that is not the case. In Python this can be written as len(guessed_letters) != len(word)
. The len()
function tells you the length of a list or string.
The second condition is met when the number of letters in the incorrect_letters
list is more than the maximum number of allowed guesses. So our loop needs to also run while that is not the case. In Python this can be written as len(incorrect_letters) < MAX_WRONG_GUESSES
. We'll add another variable to our program for the MAX_WRONG_GUESSES
value.
Putting it all together, our while loop starts like this.
# Game loop.
MAX_WRONG_GUESSES = 5
while len(guessed_letters) != len(word) and len(incorrect_letters) < MAX_WRONG_GUESSES:
Inside our loop we will need to do four things:
- Show the word to the player.
- Show the list of wrong guesses.
- Get a guess from the player.
- Record whether the guess is correct or incorrect.
Let's tackle them in order.
Showing the word
We need to show the word to the player, but only the letters they have correctly guessed should be revealed. The rest of the letters should be replaced by a dash -
character. Let's write a function that does this for us. It will take two arguments, the word to show and a list of correctly guessed letters.
def show_word(word, letters):
print("Word: ", end="")
for letter in word:
if letter in letters:
print(letter, end="")
else:
print("-", end="")
print()
We loop over each letter in the word by using a for
loop. We then check if the letter is in the list of letters using the in
operator. If it is, we print it. If not, we print a dash instead. We use the end=""
argument in the print
calls to stop a newline from being printed.
Showing the wrong guesses
Let's write another function to show the wrong guesses. This one is much simpler than the last. It takes a single argument, the list of wrong guesses, and uses a for
loop again to print out each one.
def show_wrong_guesses(guesses):
print("Wrong guesses: ", end="")
for letter in guesses:
print(letter, end="")
print()
Getting the player's guess
Our next function is going to get a guess from the player. To make sure that the player actually types something in we are going to use a while
loop. We will keep asking them for a guess until they type one in. Finally a guess should just be a single letter, so we only return the first character they type in.
def get_letter():
letter = ""
while letter == "":
letter = input("Guess a letter > ")
return letter[0]
Recording the guess
Once we have a letter from the player we can decide which of our two lists we should add it too. If the letter is in the word, it is a correct guess. We can add it to the guessed_letters
list using the append()
method. If it is not in the word, it should be added to the incorrect_letters
list. But we should also check if the letter has already been guessed before. We do this by checking if it is one of the lists with the in
operator. (This way each guess is only recorded once.)
if letter in word and letter not in guessed_letters:
guessed_letters.append(letter)
elif letter not in incorrect_letters:
incorrect_letters.append(letter)
Finishing the game
Last but not least we need to display a suitable message to the player when the game loop ends. Depending on whether they guessed the word or not, the message will be one of success or failure. How do we know if they guessed the word correctly? The number of letters in the guessed_letters
list will be the same length as the word.
# End of game message.
if len(guessed_letters) == len(word):
show_word(word, guessed_letters)
print(f"Well done! You guessed the word with {len(incorrect_letters)} wrong guesses.")
else:
print(f"Too many wrong guesses! The word was '{word}'")
That's it! We have a complete game of Hangman. To make the game harder or easier, change the number of wrong guesses allowed.
Complete program listing
def show_word(word, letters):
print("Word: ", end="")
for letter in word:
if letter in letters:
print(letter, end="")
else:
print("-", end="")
print()
def show_wrong_guesses(guesses):
print("Wrong guesses: ", end="")
for letter in guesses:
print(letter, end="")
print()
def get_letter():
letter = ""
while letter == "":
letter = input("Guess a letter > ")
return letter[0]
word = "something"
guessed_letters = []
incorrect_letters = []
# Game loop.
MAX_WRONG_GUESSES = 5
while len(guessed_letters) != len(word) and len(incorrect_letters) < MAX_WRONG_GUESSES:
show_word(word, guessed_letters)
show_wrong_guesses(incorrect_letters)
letter = get_letter()
if letter in word and letter not in guessed_letters:
guessed_letters.append(letter)
elif letter not in word and letter not in incorrect_letters:
incorrect_letters.append(letter)
# End of game message.
if len(guessed_letters) == len(word):
show_word(word, guessed_letters)
print(f"Well done! You guessed the word with {len(incorrect_letters)} wrong guesses.")
else:
print(f"Too many wrong guesses! The word was '{word}'")
Top comments (0)