Hangman Game import random # List of words for the game words = ["python", "programming", "hangman", "computer", "code", "game", "challenge"] # Choose a random word from the list word = random.choice(words) # Initialize a list to store the guessed letters guessed_letters = [] # Initialize the number of guesses allowed max_attempts = 6 attempts = 0 print("Welcome to Hangman!") print("Try to guess the word. You have 6 attempts.") # Loop until the player guesses the word or runs out of attempts while attempts < max_attempts: # Display the word with dashes for unguessed letters display_word = "" for letter in word: if letter in guessed_letters: display_word += letter + " " else: display_word += "_ " print(display_word) # Ask the player to guess a letter guess = input("Enter a letter: ").lower() # Check if the letter has already been guessed if guess in guessed_letters: print("You've already guessed that letter. Try again.") continue # Add the guess to the list of guessed letters guessed_letters.append(guess) # Check if the guess is in the word if guess in word: print("Correct!") else: print("Incorrect!") attempts += 1 # Check if the player has guessed all the letters if all(letter in guessed_letters for letter in word): print(f"Congratulations! You've guessed the word '{word}'!") break # If the player runs out of attempts, reveal the word if attempts == max_attempts: print(f"Sorry, you've run out of attempts. The word was '{word}'. Better luck next time!")