import random
Paste WY5vjL7g · Public and searchable. Times use the creator timezone (UTC).
import random
def play_hangman():
# Word list for the game
words = ["python", "developer", "interface", "argument", "compiler", "variable", "database"]
word = random.choice(words).upper()
guessed_letters = set()
incorrect_guesses = 0
max_turns = 6
# Visual stages for the hangman ascii art
stages = [
"""
--------
| |
|
|
|
|
- - - - - - -
""",
"""
--------
| |
| O
|
|
|
- - - - - - -
""",
"""
--------
| |
| O
| |
| |
|
- - - - - - -
""",
"""
--------
| |
| O
| /|
| |
|
- - - - - - -
""",
"""
--------
| |
| O
| /|\\
| |
|
- - - - - - -
""",
"""
--------
| |
| O
| /|\\
| |
| /
- - - - - - -
""",
"""
--------
| |
| O
| /|\\
| |
| / \\
- - - - - - -
"""
]
print("Welcome to Hangman!")
# Main game loop
while incorrect_guesses < max_turns:
print(stages[incorrect_guesses])
# Display the current hidden word progress
display_word = [letter if letter in guessed_letters else "_" for letter in word]
print(f"Word: {' '.join(display_word)}")
print(f"Guessed letters: {', '.join(sorted(guessed_letters)) if guessed_letters else 'None'}")
# Win condition check
if "_" not in display_word:
print(f"\n🎉 Congratulations! You guessed the word: {word}")
return
# Player input and validation
guess = input("Guess a letter: ").strip().upper()
if len(guess) != 1 or not guess.isalpha():
print("❌ Invalid input. Please type a single letter.")
continue
if guess in guessed_letters:
print(f"⚠️ You already guessed '{guess}'. Try another one.")
continue
guessed_letters.add(guess)
# Process the guess outcome
if guess in word:
print(f"✅ Good job! '{guess}' is in the word.")
else:
print(f"❌ Oops! '{guess}' is not in the word.")
incorrect_guesses += 1
# Lose condition reached
print(stages[incorrect_guesses])
print(f"\n💀 Game Over! You ran out of turns. The word was: {word}")
# Run the game
if __name__ == "__main__":
play_hangman()