import random

# List of words for the game
word_list = ["hangman", "python", "computer", "programming", "developer", "coding"]

def choose_word():
    return random.choice(word_list)

def display_word(word, guessed_letters):
    display = ""
    for letter in word:
        if letter in guessed_letters:
            display += letter + " "
        else:
            display += "_ "
    return display

def draw_hangman(incorrect_guesses):
    hangman_art = [
        """
           _________
          |         |
          |
          |
          |
          |
        """,
        """
           _________
          |         |
          |         O
          |
          |
          |
        """,
        """
           _________
          |         |
          |         O
          |         |
          |
          |
        """,
        """
           _________
          |         |
          |         O
          |        /|
          |
          |
        """,
        """
           _________
          |         |
          |         O
          |        /|\\
          |
          |
        """,
        """
           _________
          |         |
          |         O
          |        /|\\
          |        /
          |
        """,
        """
           _________
          |         |
          |         O
          |        /|\\
          |        / \\
          |
        """
    ]
    return hangman_art[incorrect_guesses]

def hangman():
    word = choose_word()
    guessed_letters = []
    incorrect_guesses = 0
    max_attempts = 6

    print("Welcome to Hangman!")

    while True:
        print("\nWord to guess: ", display_word(word, guessed_letters))
        print(draw_hangman(incorrect_guesses))

        guess = input("Guess a letter: ").lower()

        if guess in guessed_letters:
            print("You've already guessed that letter. Try again.")
            continue

        guessed_letters.append(guess)

        if guess not in word:
            incorrect_guesses += 1
            print("Incorrect guess!")
            if incorrect_guesses == max_attempts:
                print("\nSorry, you've run out of attempts. The word was:", word)
                break

        if all(letter in guessed_letters for letter in word):
            print("\nCongratulations! You've guessed the word:", word)
            break

    play_again = input("Do you want to play again? (yes/no): ")
    if play_again.lower() == "yes":
        hangman()

hangman()