import random
from hangman_art import stages, logo
from hangman_words import word_list
from replit import clear

print(logo)
game_is_finished = False
lives = len(stages) - 1

chosen_word = random.choice(word_list)
word_length = len(chosen_word)

display = ["_"] * len(chosen_word)

while not game_is_finished:
    guess = input("Guess a letter: ").lower()
    clear()

    if guess in display:
        print(f"You've already guessed {guess}")

    for position in range(word_length):
        letter = chosen_word[position]
        if letter == guess:
            display[position] = letter
    print(' '.join(display))

    if guess not in chosen_word:
        lives -= 1
        print(
            f"You guessed {guess}\nThat's not in the word.\nYou lose a life. {lives} lifes left")
        if lives == 0:
            game_is_finished = True
            print(f"You lose.\nThe word was {chosen_word}")

    if not "_" in display:
        game_is_finished = True
        print("You win.")

    print(stages[lives])
