Programmeerimine

import random as r


def select_difficulty(words_hard, words_easy) -> list:

    difficulty = input("Please select difficulty ('h' for 'hard' and 'e' for 'easy'): ")

    if difficulty == 'h':
        words = words_hard
    elif difficulty == 'e':
        words = words_easy
    else:
        raise ValueError("Not allowed input")

    return words


def update_word(word, guess_word, char):

    for i in range(len(word)):
        if word[i] == char:
            guess_word[i] = char


def track_game_state(word, guess_word, guesses) -> bool:

    if "".join(guess_word) == word:
        print("\nYou won!")
        print(f"The word is: '{word}'")
        return True

    if guesses == 0:
        print("\nYou lost!")
        print(f"The word was '{word}'")
        return True

    return False


def game(words_hard: list, words_easy: list):

    words = select_difficulty(words_hard, words_easy)

    word = r.choice(words)
    guesses = len(word)
    guess_word = ["*"] * len(word)

    finished = False

    while not finished:
        print(f"\nGuesses left: {guesses}")
        print(f"The word is: {''.join(guess_word)}")

        char = input("Please enter single character: ")

        if char not in word:
            print(f"The character '{char}' is not in the word!")
            guesses -= 1
        else:
            update_word(word, guess_word, char)

        finished = track_game_state(word, guess_word, guesses)


def main():

    hard = [
        "table", "star", "root", "flat", "turtle", "fish", "goat", "rocket",
        "dragon", "power", "source", "program", "wheel", "candle", "dog", "cat", "branch"
    ]

    easy = [
        "dragonborn", "monosaccharide", "disposable", "environment",
        "implementation", "disorientation", "carbohydrate", "disrespectful", "dominant"
    ]

    game(words_hard=hard, words_easy=easy)


if __name__ == '__main__':
    main()