"""Play a simple game of Hangman using sets to track guesses."""

import random

WORDS = [
    "python", "banana", "computer", "hangman",
    "programming", "challenge", "practice",
]
LIMIT = 5


def main():
    """Run the Hangman game."""
    word = random.choice(WORDS)

    # Display the initial state of the word.
    print("The word has", len(word), "letters.")
    print("You have", LIMIT, "guesses.")

    # Track all guesses, unique letters remaining, and wrong guesses.
    guessed = set()
    remaining = set(word)
    wrong = set()

    # Game loop: continue until all letters guessed or reached limit.
    while len(remaining) > 0 and len(wrong) < LIMIT:

        # Display the current state of the word.
        print()
        for letter in word:
            if letter in guessed:
                print(letter, end=" ")
            else:
                print("_", end=" ")
        print()

        # Display the wrong guesses and remaining guesses.
        print("Wrong guesses:", wrong)
        print("Guesses left:", LIMIT - len(wrong))

        # Prompt the user for a letter and process the guess.
        letter = input("Guess a letter: ").lower()
        if letter in guessed:
            print("You already guessed that letter!")
        else:
            guessed.add(letter)

            # Check if the guessed letter is in the word.
            if letter in word:
                remaining.discard(letter)
                print("Correct!")
            else:
                wrong.add(letter)
                print("Incorrect!")

    # Final outcome of the game.
    print()
    if len(remaining) == 0:
        print("You win! The word was", word)
    else:
        print("You lose! The word was", word)


if __name__ == "__main__":
    main()
