# Hangman Game

For this challenge, you are going to create a game of hangman. The computer will pick a word randomly within a given list of words. The player will have to guess the word by suggesting one letter at a time and the computer will automatically check if the letter given is included in the word to guess. If not it will add one element to the hanged stick man drawing.

#### Complete the Code

We have started the code for you, but you will need to complete this code:

Step 1: Display the word replacing “_” with letters that have been guessed correctly. You can use the following flowchart to help you with this step:

Step 2: Display the hangman based on the number of lives remaining.
Step 3: Use a loop to repeat the above steps. The game should stop when the user guessed the words or when they have lost all their lives.

[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

Python Code

#### Python Code

```#Hangman - www.101computing.net/hangman-game/
import random

def drawHangman(life):
if life==0:
print ("                 ")
print ("     ________    ")
print ("     | /    |    ")
print ("     |/    (~)   ")
print ("     |      |    ")
print ("     |   ---+--- ")
print ("     |      |    ")
print ("     |     / \   ")
print ("     |   _/   \_ ")
print ("     |           ")
print ("    _|_          ")
print ("                 ")
print ("  ###############")
print ("  #             #")
print ("  #  GAME OVER  #")
print ("  #             #")
print ("  ###############")
elif life==1:
print ("                 ")
print ("     ________    ")
print ("     | /    |    ")
print ("     |/    (~)   ")
print ("     |      |    ")
print ("     |   ---+--- ")
print ("     |      |    ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("    _|_          ")
elif life==2:
print ("                 ")
print ("     ________    ")
print ("     | /    |    ")
print ("     |/    (~)   ")
print ("     |      |    ")
print ("     |      |    ")
print ("     |      |    ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("    _|_          ")
elif life==3:
print ("                 ")
print ("     ________    ")
print ("     | /    |    ")
print ("     |/    (~)   ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("    _|_          ")
elif life==4:
print ("                 ")
print ("     ________    ")
print ("     | /        ")
print ("     |/          ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("    _|_          ")
elif life==5:
print ("                 ")
print ("     _           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("     |           ")
print ("    _|_          ")
else:
print ("                 ")

#Initialise the game
listOfWords=["mouse","laptop","desktop","printer","keyboard"]
wordToGuess=random.choice(listOfWords).upper()

numberOfLives=6
wordSoFar=""

for letter in wordToGuess:
wordSoFar = wordSoFar + "_ "

print(wordSoFar)

#Start Playing
while (numberOfLives>0 and wordSoFar!=wordToGuess):
print("\n")
letter = input("What letter?").upper()

if letter in wordToGuess:
print("Good Guess")
wordSoFar=""
for letter wordToGuess:
wordSoFar = wordSoFar + letter
else:
wordSoFar = wordSoFar + "_ "
print(wordSoFar)
else:
print("You have lost a life")
numberOfLives-=1
drawHangman(numberOfLives)

if numberOfLives>0:
print("Well Done!")
else:
print("Game Over!")
```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 10

No votes so far! Be the first to rate this post.

As you found this challenge interesting...