Digital Literacy JH -2
Python script to create simple hangman game. Part 1
Python script to create simple hangman game. Part 2
Digital Literacy JH -2
Explanation for the python script: - The `play_hangman()` function is the
1. **Importing the `random` module:** main game logic.
- The script starts by importing the 4. **Variables initialization:**
`random` module, which provides functions - `word`: The selected random word that
for generating random numbers and the player needs to guess.
selecting random elements from a list.
- `guessed_letters`: A list to store the
2. **Defining the `get_word()` function:** letters guessed by the player.
- The `get_word()` function selects a - `wrong_guesses`: The number of
random word from a predefined list of words incorrect guesses made by the player.
(`word_list`) and returns it.
- `max_wrong_guesses`: The maximum
number of wrong guesses allowed (6 in this
3. **Defining the `play_hangman()` case).
function:**
Digital Literacy JH -2
- `game_over`: Boolean variable to control 9. **Checking player input:**
the game loop.
- If the guessed letter is already in the
5. **Main game loop:** `guessed_letters` list, a message is
displayed, asking the player to try again.
- The game loop continues until
`game_over` becomes `True`. 10. **Updating game variables:**
6. **Displaying the word to guess:** - The guessed letter is added to
`guessed_letters`.
- The script generates a display string
`display_word` that shows the current state - If the guessed letter is not in the word,
of guessed and hidden letters in the word. the `wrong_guesses` counter is incremented.
If the player reaches the maximum number
7. **Printing game information:**
of wrong guesses, the game ends with a loss
- It displays the current state of the word message.
being guessed and the letters already
11. **Checking game state:**
guessed by the player.
- If the guessed word does not contain any
8. **Taking player input:**
blank spots (`_`), the player wins, and the
- The player is prompted to enter a letter as game ends with a congratulatory message.
a guess.
12. **Playing the game:**
- The `play_hangman()` function is called
to start the game.