# Hangman Game

Let's code the hangman game step by step.

1. Now we need a list of words to play right! we can add any number of words and any words.
    
    <details data-node-type="hn-details-summary"><summary>List in Python</summary><div data-type="detailsContent">A list in Python is an ordered and mutable collection of items enclosed within square brackets.</div></details>
    
    Example : `Numbers = [1, 2, 3, 4, 5]`
    
2. Next, we have to select a word from the list, which happens randomly.
    
    <details data-node-type="hn-details-summary"><summary>Import Random Module</summary><div data-type="detailsContent">The <code>random</code> module in Python is a built-in library that provides functions for generating random numbers and performing random operations. It's commonly used in applications where randomness or unpredictability is needed, such as games, simulations, and data shuffling.</div></details>
    
    1. Initialize variables to be used in the game throughout the code.
        
        <details data-node-type="hn-details-summary"><summary>Initializing variables means assigning an initial value to a variable before you use it in your code. This is an essential step in programming, as it allows you to store and manipulate data.</summary><div data-type="detailsContent"></div></details>
        
        Example : `my_variable = 42`
        
        In this example, we've created a variable called `my_variable` and assigned it the value `42`. Python will automatically determine the data type based on the assigned value.
        
        ```python
        display = []
        for _ in range(word_length):
            display += "_"
        ```
        
    2. Visually (Manually) designing the hangman
        
        ```python
        # Hangman stages
        stages = [
            """
             -----
            |     |
                  |
                  |
                  |
                  |
            """,
            """
             -----
            |     |
            O     |
                  |
                  |
                  |
            """,
            """
             -----
            |     |
            O     |
            |     |
                  |
                  |
            """,
            """
             -----
            |     |
            O     |
           /|     |
                  |
                  |
            """,
            """
             -----
            |     |
            O     |
           /|\\    |
                  |
                  |
            """,
            """
             -----
            |     |
            O     |
           /|\\    |
           /      |
                  |
            """,
            """
             -----
            |     |
            O     |
           /|\\    |
           / \\    |
                  |
            GAME OVER!
            """
        ]
        ```
        
    

5.We have to loop through and check if the user entered input is eqaul to the randomaly generated word.

```python
# Game loop
game_over = False
attempts = len(stages) - 1

while not game_over:
    # Display the current state of the word
    print(" ".join(display))

    # Get a guess from the player
    guess = input("Guess a letter: ").lower()

    if guess in guessed_letters:
        print(f"You've already guessed '{guess}'. Try again.")
        continue

    if guess in chosen_word:
        for i in range(word_length):
            if chosen_word[i] == guess:
                display[i] = guess
    else:
        print(f"'{guess}' is not in the word.")
        guessed_letters.append(guess)
        attempts -= 1
        print(stages[attempts])

    if "_" not in display:
        print("Congratulations! You guessed the word.")
        game_over = True
    elif attempts == 0:
        print(f"Out of attempts. The word was '{chosen_word}'.")
        game_over = True
```

1. Let's Complete the code.
    
    ```python
    import random
    
    # List of words to choose from
    word_list = ["paris", "spain", "seoul", "india", "busan", "america", "poland", "london"]
    
    # Select a random word from the list
    chosen_word = random.choice(word_list)
    word_length = len(chosen_word)
    
    # Initialize game variables
    display = []
    for _ in range(word_length):
        display += "_"
    
    # Hangman stages
    stages = [
        """
         -----
        |     |
              |
              |
              |
              |
        """,
        """
         -----
        |     |
        O     |
              |
              |
              |
        """,
        """
         -----
        |     |
        O     |
        |     |
              |
              |
        """,
        """
         -----
        |     |
        O     |
       /|     |
              |
              |
        """,
        """
         -----
        |     |
        O     |
       /|\\    |
              |
              |
        """,
        """
         -----
        |     |
        O     |
       /|\\    |
       /      |
              |
        """,
        """
         -----
        |     |
        O     |
       /|\\    |
       / \\    |
              |
        GAME OVER!
        """
    ]
    
    # Track guessed letters
    guessed_letters = []
    
    # Game loop
    game_over = False
    attempts = len(stages) - 1
    
    while not game_over:
        # Display the current state of the word
        print(" ".join(display))
    
        # Get a guess from the player
        guess = input("Guess a letter: ").lower()
    
        if guess in guessed_letters:
            print(f"You've already guessed '{guess}'. Try again.")
            continue
    
        if guess in chosen_word:
            for i in range(word_length):
                if chosen_word[i] == guess:
                    display[i] = guess
        else:
            print(f"'{guess}' is not in the word.")
            guessed_letters.append(guess)
            attempts -= 1
            print(stages[attempts])
    
        if "_" not in display:
            print("Congratulations! You guessed the word.")
            game_over = True
        elif attempts == 0:
            print(f"Out of attempts. The word was '{chosen_word}'.")
            game_over = True
    ```
    
    1. Output time
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695654047031/09c8463d-a68b-4a48-8ad5-98c1bf83698c.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695654067556/79c9d7fb-5ae4-4cd8-9fc9-6db5c27d7735.png align="center")
