Friday 10/17/2025

today i made a small project of hangman that took 20mins using python on vscode i will be adding code and pictures below

words = ("apple", "orange", "banana", "coconut", "pineapple")

the code above was the list i used for words(cheat sheet)


def display_man(wrong_guesses):
    print("************")
    for line in hangman_art[wrong_guesses]:
        print(line)
    print("************")

def display_hint(hint):
    print("".join(hint))

def display_answer(answer):
    print("".join(answer))

def main():
    answer = random.choice(words)
    hint = ["_"] * len(answer)
    wrong_guesses = 0
    guessed_letters = set()
    is_running = True

the functions i used in the code main was the most important one

while is_running:
        display_man(wrong_guesses)
        display_hint(hint)
        guess = input("enter a letter: ").lower()

        if len(guess) != 1 or not guess.isalpha():
            print("Invalid input")
            continue

        if guess in guessed_letters:
            print(f"{guess} is already guessed")
            continue

        guessed_letters.add(guess)

        if guess in answer:
            for i in range(len(answer)):
                if answer [i] == guess:
                    hint[i] = guess

        else:
            wrong_guesses += 1

        if "_" not in hint:
            display_man(wrong_guesses)
            display_answer(answer)
            print("YOU WIN!")
            is_running = False
        elif wrong_guesses >=len(hangman_art) - 1:
            display_man(wrong_guesses)
            display_answer(answer)
            print("YOU LOSE!")
            is_running = False

and finally the while i used to make everything run the end 🙂


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *