import random

# List of words to choose from
WORD_LIST = ["apple", "banana", "cherry", "orange", "grape", "melon", "kiwi"]

# Hangman ASCII art
HANGMAN = [
    """
    +---+
    |   |
        |
        |
        |
        |
  =========
    """,
    """
    +---+
    |   |
    O   |
        |
        |
        |
  =========
    """,
    """
    +---+
    |   |
    O   |
    |   |
        |
        |
  =========
    """,
    """
    +---+
    |   |
    O   |
   /|   |
        |
        |
  =========
    """,
    """
    +---+
    |   |
    O   |
   /|\\  |
        |
        |
  =========
    """,
    """
    +---+
    |   |
    O   |
   /|\\  |
   /    |
        |
  =========
    """,
    """
    +---+
    |   |
    O   |
   /|\\  |
   / \\  |
        |
  =========
    """
]

def choose_word():
    return random.choice(WORD_LIST)

def display_word(word, guessed_letters):
    displayed_word = ""
    for letter in word:
        if letter in guessed_letters:
            displayed_word += letter
        else:
            displayed_word += "_"
    return displayed_word

def get_guess(guessed_letters):
    while True:
        guess = input("Guess a letter: ").lower()
        if len(guess) != 1 or not guess.isalpha():
            print("Please enter a single letter.")
        elif guess in guessed_letters:
            print("You've already guessed that letter.")
        else:
            return guess

def play_hangman():
    word = choose_word()
    guessed_letters = []
    incorrect_guesses = 0

    print("Welcome to Hangman!")
    print(display_word(word, guessed_letters))

    while incorrect_guesses < len(HANGMAN) - 1:
        guess = get_guess(guessed_letters)
        guessed_letters.append(guess)

        if guess in word:
            print("Correct!")
        else:
            print("Incorrect!")
            incorrect_guesses += 1

        displayed_word = display_word(word, guessed_letters)
        print(displayed_word)

        if "_" not in displayed_word:
            print("Congratulations! You guessed the word:", word)
            break

        print(HANGMAN[incorrect_guesses])

    else:
        print("Sorry, you ran out of guesses. The word was:", word)

    play_again = input("Do you want to play again? (yes/no): ").lower()
    if play_again == "yes":
        play_hangman()

if __name__ == "__main__":
    play_hangman()
