How to make hangman

import random

words = [‘python’, ‘java’, ‘kotlin’, ‘javascript’, ‘ruby’, ‘swift’]

#randomly choose a word from this list

chosen_word = random.choice(words)

word_display = [‘_’ for _ in chosen_word]  # Create a list of underscores

attempts = 8  # Number of allowed attempts

print(“Welcome to hangman”)

while attempts > 0 and ‘_’ in word_display:

    print(“\n” + ‘ ‘.join(word_display))

    guess = input(“Guess a letter: “).lower()

    if guess in chosen_word:

        for index, letter in enumerate(chosen_word):

            if letter == guess:

                word_display[index] = guess # reveal letter

    else:

        print(“That letter doesnt appear in the word!”)

        attempts -=1

# Game conclusion

if ‘_’ not in word_display:

    print(“You guessed the word!”)

    print(‘ ‘.join(word_display))

    print(“You survived”)

else:

    print(“You ran out of attempts. The word was: ” + chosen_word)

    print(“You lost!”)

its a really simple and easy project to make


Comments

Leave a Reply

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