Hangman is a popular yet grim intellectual game. A cruel computer hides a word from you. Letter by letter you try to guess it. If you fail, you'll be hanged, if you win, you'll survive. See also: Wikipedia
In this game the player has to guess a word, letter by letter, in a limited number of attempts. Make a program that plays Hangman with you – and good luck with the guessing!
Requirements:
- Python 3.7
- To run the tests: https://github.com/hyperskill/hs-test-python
python hangman.py
You probably played the game at least once in your life; now you can actually create this game yourself!
Let's have a brief overview of what you are going to build during this project. Here is what the gameplay should look like:
- In the main menu, a player can choose to either play or exit the game;
- If the user chooses to play, the computer picks a word from a list: this will be the riddle;
- The computer asks the player to enter a letter that (s)he thinks is in the word;
- If there's no such letter in the word and this letter hasn't been tried before, the computer counts it as a miss. A player can afford only up to 8 misses before the game is over;
- If the letter does occur in the word, the computer notifies the player. If there are letters left to guess, the computer invites the player to go on.
- When the entire word is uncovered, it's a victory! The game calculates the final score and goes back to the main menu.
It may sound complex, but the project is split into small stages with hints to see you through. The final product is sure to be replayable and quite engaging!
Let's start with an announcement that will greet the player. You already know how to print something using Python: try to apply your knowledge to intrigue your friends with your game announcement!
In this stage you should write a program that prints the lines as shown in the example below:
Output:
H A N G M A N
The game will be available soon.
At this stage, you will create a real game. It will be simple, but there will be two possible outcomes (you can see in the examples below how they look like). Let's first print a welcome message and then ask a player to guess the word we set for the game. If our player manages to guess the exact word, the game reports "win"; otherwise it will "hang" the player.
- Ask a player for a possible word.
- Print
You survived!
if the user guessed the word. - Print
You are hanged!
if the user haven't guessed the word
python
should be the correct word to win the game.The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
Example 1
H A N G M A N
Guess the word: > python
You survived!
Example 2
H A N G M A N
Guess the word: > java
You are hanged!
If there is a predefined word, the game isn't replayable: you already know the word, so it makes no sense to guess it. At this stage, let's make the game more challenging by choosing a word from a special list with a variety of options. This way, our game won't be just a one-time entertainment.
- Create the following word list:
'python', 'java', 'kotlin', 'javascript'
. - Program the game to choose a random word from it. You can enter more words, but let's stick to these four for now.
The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
Example 1, the computer randomly chose python
from the list.
H A N G M A N
Guess the word: > python
You survived!
Example 2, the computer randomly chose something other than python
from the list.
H A N G M A N
Guess the word: > python
You are hanged!
Example 3, the computer randomly chose something other than kotlin
from the list.
H A N G M A N
Guess the word: > kotlin
You are hanged!
Now our game has become quite hard, and your chances of guessing the word depend on the size of the list. In our case with four words, there is only a 25% chance, so let's have mercy on the player and add a hint for them.
- As in the previous stage, you should use the following word list:
'python', 'java', 'kotlin', 'javascript'
- Show the first 3 letters after the computer chose a word from the list. Hidden letters should be replaced with hyphens (
"-"
).
The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
Example 1
H A N G M A N
Guess the word jav-: > java
You survived!
Example 2
H A N G M A N
Guess the word pyt---: > pythia
You are hanged!
Let's make the game iterative. It's time to make it resemble the classical Hangman a bit more: a player should guess letters in the word instead of typing the entire word at once. If the player guesses a letter, it should be uncovered in the word. For now, start with the defeat case and add 8 tries to guess a letter that appears in the word. When the player runs out of attempts, the game ends.
Later we will determine the winning conditions, but in this stage, let's see how well our player guesses the word on every attempt.
Now your game should work the following way:
- A player has exactly 8 tries and enters letters. If a player has more tries but he actually guessed the word, it doesn't mean anything.
- If the letter doesn't occur in the word, the computer takes one try away, even if the user already inputted this letter before.
- If the player doesn't have any more attempts, the game should end and the program should show a losing message. Otherwise, the player can continue to input letters.
- Also, use our previous word list:
'python', 'java', 'kotlin', 'javascript'
so that your program can be tested more reliably.
The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
Example 1
H A N G M A N
----------
Input a letter: > a
-a-a------
Input a letter: > i
-a-a---i--
Input a letter: > o
No such letter in the word
-a-a---i--
Input a letter: > z
No such letter in the word
-a-a---i--
Input a letter: > p
No such letter in the word
-a-a---ip-
Input a letter: > p
-a-a---ip-
Input a letter: > h
No such letter in the word
-a-a---ip-
Input a letter: > k
No such letter in the word
Thanks for playing!
We'll see how well you did in the next stage
Example 2
H A N G M A N
----
Input a letter: > j
j---
Input a letter: > i
No such letter in the word
j---
Input a letter: > g
No such letter in the word
j---
Input a letter: > o
No such letter in the word
j---
Input a letter: > a
ja-a
Input a letter: > v
java
Input a letter: > a
java
Input a letter: > j
Thanks for playing!
We'll see how well you did in the next stage
The recent version of the game is not as fun until we don't handle the player's victory. A player has 8 attempts to guess letters and its number is reduced even if the letter was correct.
Now a player will have a lot of attempts and is limited only by the number of mistakes they make. A player can be mistaken 8 times and wins when all the letters are guessed and there are still some tries left. If the player uses the last try and actually guesses the word, they are lucky then!
The player starts the game with 8 "lives", that is our player can input the wrong letter 8 times.
- Print
No such letter in the word
and reduce the attempts count if the word guessed by the program doesn't contain this letter. - Print
No improvements
and reduce the attempts count if the guessed word contains this letter but the user tried this letter before. - The attempts count should be decreased only if there are no letters to uncover.
The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
Example 1
H A N G M A N
------
Input a letter: > t
--t---
Input a letter: > z
No such letter in the word
--t---
Input a letter: > t
No improvements
--t---
Input a letter: > t
No improvements
--t---
Input a letter: > y
-yt---
Input a letter: > x
No such letter in the word
-yt---
Input a letter: > y
No improvements
-yt---
Input a letter: > p
pyt---
Input a letter: > p
No improvements
pyt---
Input a letter: > q
No such letter in the word
pyt---
Input a letter: > p
No improvements
You are hanged!
Example 2
H A N G M A N
----
Input a letter: > j
j---
Input a letter: > i
No such letter in the word
j---
Input a letter: > g
No such letter in the word
j---
Input a letter: > g
No such letter in the word
j---
Input a letter: > g
No such letter in the word
j---
Input a letter: > g
No such letter in the word
j---
Input a letter: > a
ja-a
Input a letter: > v
java
You guessed the word!
You survived!
Now that we are done with the basics, let's work on some details.
In the previous stage if the user entered the same letter twice or typed a cyrillic letter, the program reduced the number of attempts regardless if this was a correct letter or not. But it is not fair to the user, isn't it? He gains no additional information about the situation on the field yet the program still reduces his attempts count. Let's fix it!
- If the user enters the same letter twice then the program should output
You already typed this letter
. - Also, you should check if the user prints an English lowercase letter or not. If not, the program should print
It is not an ASCII lowercase letter
. - Also, you should check if the user printed exactly one letter. If not, the program should print
You should input a single letter
. Remember that zero is also not one! - Note that all these three errors should not reduce attempts count!
The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
Example 1
H A N G M A N
----------
Input a letter: > a
-a-a------
Input a letter: > i
-a-a---i--
Input a letter: > o
No such letter in the word
-a-a---i--
Input a letter: > o
You already typed this letter
-a-a---i--
Input a letter: > p
-a-a---ip-
Input a letter: > p
You already typed this letter
-a-a---ip-
Input a letter: > h
No such letter in the word
-a-a---ip-
Input a letter: > k
No such letter in the word
-a-a---ip-
Input a letter: > a
You already typed this letter
-a-a---ip-
Input a letter: > z
No such letter in the word
-a-a---ipt
Input a letter: > t
-a-a---ipt
Input a letter: > x
No such letter in the word
-a-a---ipt
Input a letter: > b
No such letter in the word
-a-a---ipt
Input a letter: > d
No such letter in the word
-a-a---ipt
Input a letter: > w
No such letter in the word
You are hanged!
Example 2
H A N G M A N
----
Input a letter: > j
j---
Input a letter: > i
No such letter in the word
j---
Input a letter: > +
It is not an ASCII lowercase letter
j---
Input a letter: > A
It is not an ASCII lowercase letter
j---
Input a letter: > ii
You should input a single letter
j---
Input a letter: > ++
You should input a single letter
j---
Input a letter: >
You should input a single letter
j---
Input a letter: > g
No such letter in the word
j---
Input a letter: > a
ja-a
Input a letter: > v
You guessed the word java!
You survived!
We're almost done!
Let's add more flavor to the game by adding a suggestion to replay after the current game session ends.
- The game starts with a menu where a player can choose to either play or exit.
- Print
Type "play" to play the game, "exit" to quit:
and ask again if the player inputs something else. - If the user chooses to play, the game starts.
The greater-than symbol followed by space (>
) represents the user input. Notice that it's not the part of the input.
H A N G M A N
Type "play" to play the game, "exit" to quit: > play
----------
Input a letter: > a
-a-a------
Input a letter: > i
-a-a---i--
Input a letter: > o
No such letter in the word
-a-a---i--
Input a letter: > o
You already typed this letter
-a-a---i--
Input a letter: > p
-a-a---ip-
Input a letter: > p
You already typed this letter
-a-a---ip-
Input a letter: > h
No such letter in the word
-a-a---ip-
Input a letter: > k
No such letter in the word
-a-a---ip-
Input a letter: > a
You already typed this letter
-a-a---ip-
Input a letter: > z
No such letter in the word
-a-a---ipt
Input a letter: > t
-a-a---ipt
Input a letter: > x
No such letter in the word
-a-a---ipt
Input a letter: > b
No such letter in the word
-a-a---ipt
Input a letter: > d
No such letter in the word
-a-a---ipt
Input a letter: > w
No such letter in the word
You are hanged!
Type "play" to play the game, "exit" to quit: > exit