> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/notes/programming/python/examples-projects/hangman.md).

# Hangman

Guess the word before the man is hung!

{% tabs %}
{% tab title="art" %}

```python
stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

logo2 = '''
█▀▀ █▀█ █▀█ █▀▄   █░█░█ █▀█ █▀█ █▄▀ █
█▄█ █▄█ █▄█ █▄▀   ▀▄▀▄▀ █▄█ █▀▄ █░█ ▄
'''

logo3 = '''
█░█░█ █▀▀ █░░ █▀▀ █▀█ █▀▄▀█ █▀▀   ▀█▀ █▀█ ▀
▀▄▀▄▀ ██▄ █▄▄ █▄▄ █▄█ █░▀░█ ██▄   ░█░ █▄█ ▄

██╗░░██╗░█████╗░███╗░░██╗░██████╗░███╗░░░███╗░█████╗░███╗░░██╗
██║░░██║██╔══██╗████╗░██║██╔════╝░████╗░████║██╔══██╗████╗░██║
███████║███████║██╔██╗██║██║░░██╗░██╔████╔██║███████║██╔██╗██║
██╔══██║██╔══██║██║╚████║██║░░╚██╗██║╚██╔╝██║██╔══██║██║╚████║
██║░░██║██║░░██║██║░╚███║╚██████╔╝██║░╚═╝░██║██║░░██║██║░╚███║
╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝░╚═════╝░╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝'''
```

{% endtab %}

{% tab title="wordlist" %}

```
WORDLIST = ['computer', 'banana', 'elephant', 'guitar', 'oxygen', 'jacket', 'diamond', 'pancake',
            'unicorn', 'bicycle', 'lighthouse', 'telescope', 'password', 'avalanche', 'crocodile',
            'fireworks', 'keyboard', 'kangaroo', 'zebra', 'pyramid', 'galaxy', 'octopus', 'tambourine',
            'helicopter', 'tornado', 'pineapple', 'parrot', 'waterfall', 'cocktail', 'volcano', 'avalanche',
            'squirrel', 'flamingo', 'cactus', 'dinosaur', 'rocket', 'umbrella', 'vampire', 'coconut',
            'haystack', 'tiramisu', 'igloo', 'origami', 'moonlight', 'gondola', 'toucan', 'saxophone',
            'oasis', 'yo-yo', 'espresso']

```

{% endtab %}

{% tab title="main" %}

```python
import random
import art
from wordlist import WORDLIST

lives = 6
used_letters = []
word = []

chosen_word = random.choice(WORDLIST)
word.extend(chosen_word)

welcome_message = art.logo3
win_message = art.logo2
start_photo = art.stages[lives]

print(welcome_message)
print("To win, guess the word!")
print(start_photo)

# Initialize displayed_word with underscores
displayed_word = ['_' for _ in chosen_word]

while lives != 0:
    print("\nused_letters: ", used_letters)
    print(' '.join(displayed_word))
    guess_letter = input("Guess a letter: ").lower()

    if not guess_letter.isalpha() or len(guess_letter) != 1:
        print("Input only a single letter.")
        continue

    if guess_letter not in used_letters:
        used_letters.append(guess_letter)
    else:
        print("Letter already used")
        continue

    if guess_letter in chosen_word:
        for index, letter in enumerate(chosen_word):
            if letter == guess_letter:
                displayed_word[index] = guess_letter
    else:
        lives -= 1
        start_photo = art.stages[lives]

    print(start_photo)

    if '_' not in displayed_word:
        print("Congratulations! You've guessed the word!")
        print(win_message)
        break

if lives == 0:
    print("You've run out of lives. The word was:", ''.join(chosen_word))

```

{% endtab %}
{% endtabs %}

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUwIKQPujWqWSVFxQaX98%2Fuploads%2FC14YX0B0DkjrNvX1CYqo%2Fday7.mp4?alt=media&token=3a8a28ad-a67f-47f4-b095-52beb89d6b5e>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://digitalgarden.batamladen.com/notes/programming/python/examples-projects/hangman.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
