latestanagramcode

 avatar
unknown
plain_text
a year ago
1.3 kB
4
Indexable
import random

def load_word_list(file_path):
    with open(file_path, 'r') as file:
        word_list = file.read().splitlines()
    return word_list

def select_word(word_list):
    return random.choice(word_list)

def generate_anagram(word):
    characters = list(word)
    random.shuffle(characters)
    return ''.join(characters)

def play_anagram_game(word_list):
    print("Welcome to the Anagram Game!")
    while True:
        word = select_word(word_list)
        anagram = generate_anagram(word)

        print("\nSolve the anagram:", anagram)

        attempts = 5
        while attempts > 0:
            guess = input("Make a guess: ").lower()
            if guess == word.lower():
                print("Correct!")
                break
            else:
                attempts -= 1
                print("Incorrect. Attempts Left:", attempts)
        
        if attempts == 0:
            print("Out of attempts. The correct word was:", word)

        choice = input("\nContinue? [y/n]: ")
        if choice.lower() != 'y':
            print("\nThank you for playing!")
            break

# Example usage
word_file_path = 'word_list.txt'  # Path to a file containing a list of words, one word per line
words = load_word_list(word_file_path)
play_anagram_game(words)