import random

# List of words to choose from
word_list = ["python", "hangman", "game", "programming", "computer", "player", "guess"]

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 display_hangman(incorrect_guesses):
    hangman_art = [
        """
         _________
        |         |
        |         O
        |        \|/
        |         |
        |        / \\
        -
        """,
        """
         _________
        |         |
        |         O
        |        \|/
        |         |
        |        / 
        -
        """,
        """
         _________
        |         |
        |         O
        |        \|/
        |         |
        |         
        -
        """,
        """
         _________
        |         |
        |         O
        |        \|
        |         |
        |        
        -
        """,
        """
         _________
        |         |
        |         O
        |         |
        |         |
        |        
        -
        """,
        """
         _________
        |         |
        |         O
        |         
        |         
        |        
        -
        """,
        """
         _________
        |         |
        |         
        |         
        |         
        |        
        -
        """
    ]
    return hangman_art[incorrect_guesses]

def hangman():
    print("Welcome to Hangman!")
    word = choose_word()
    guessed_letters = []
    incorrect_guesses = 0
    max_guesses = len(word) + 3

    while True:
        print(display_hangman(incorrect_guesses))
        print(display_word(word, guessed_letters))

        if "_" not in display_word(word, guessed_letters):
            print("Congratulations! You guessed the word:", word)
            break

        if incorrect_guesses >= 6:
            print("You ran out of guesses! The word was:", word)
            break

        guess = input("Guess a letter: ").lower()

        if len(guess) != 1 or not guess.isalpha():
            print("Please enter a single alphabetical character.")
            continue

        if guess in guessed_letters:
            print("You already guessed that letter!")
            continue

        guessed_letters.append(guess)

        if guess not in word:
            print("Incorrect guess!")
            incorrect_guesses += 1

    play_again = input("Do you want to play again? (yes/no): ").lower()
    if play_again == "yes":
        hangman()

hangman()