0% found this document useful (0 votes)
3 views

Python Script To Create Simple Hangman Game

The document explains a Python script to create a simple hangman game. It imports the random module, defines functions to select a random word and play the game, initializes variables to track the word, guesses and guesses remaining, takes player input, updates guesses and checks for wins or losses.

Uploaded by

Clarence Lexmono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Script To Create Simple Hangman Game

The document explains a Python script to create a simple hangman game. It imports the random module, defines functions to select a random word and play the game, initializes variables to track the word, guesses and guesses remaining, takes player input, updates guesses and checks for wins or losses.

Uploaded by

Clarence Lexmono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

You might also like