Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

wordle python

from random_words import RandomWords
from colorama import Fore
import enchant

print("
Try to guess the word in 5 tries. After each guess, the colour of the letters will change to show how close "
      "
your guess was to the word. If the letter is green it is in the word and it is in the correct place. If the "
      "
letter is yellow it is in the word but it is in the wrong place.")

while True:
    word = ''
    word_arr = []
    guess = ''
    guess_count = 0

    while len(word) != 5:
        word = RandomWords().random_word()

    for letter in word:
        word_arr.append(letter)

    while guess != word and guess_count != 6:

        guess = (input("
Please enter a 5 letter word: ")).lower()
        has_number = False

        for char in guess:
            has_number = char.isdigit()

        if has_number:
            print("
Error: Please enter a word")
            continue

        elif not has_number:
            words_us = enchant.Dict("en_Us")
            words_gb = enchant.Dict("en_GB")

            if len(guess) > 5:
                print("
Error: Please enter a 5 letter word")

            elif len(guess) < 5:
                print("
Error: Please enter a 5 letter word")

            elif not words_us.check(guess) and not words_gb.check(guess):
                print(f"
{guess} is not a word")

            elif len(guess) == 5 and (words_us.check(guess) or words_gb.check(guess)):
                guess_count += 1
                guess_arr = []
                guess_arr_copy = []
                word_arr_copy = []
                pos_correct = False
                letter_found = False
                temp = ''

                for letter in guess:
                    guess_arr.append(letter)
                    guess_arr_copy.append(letter)

                for letter in word_arr:
                    word_arr_copy.append(letter)

                for i in range(0, 5):

                    if guess_arr_copy[i] == word_arr_copy[i]:
                        pos = i
                        correct_letter = guess_arr[i]
                        letter_found = True
                        pos_correct = True
                        guess_arr[pos] = Fore.GREEN + str(correct_letter) + Fore.RESET
                        guess_arr_copy[i] = ['0']
                        word_arr_copy[i] = ['1']

                    elif guess_arr_copy[i] in word_arr_copy:
                        pos = guess_arr.index(guess_arr[i])
                        correct_letter = guess_arr[i]
                        letter_found = True
                        pos_correct = False
                        guess_arr[pos] = Fore.YELLOW + str(correct_letter) + Fore.RESET
                        word_arr_copy[word_arr_copy.index(guess_arr_copy[i])] = ['1']
                        guess_arr_copy[i] = ['0']

                print(guess_arr[0], guess_arr[1], guess_arr[2], guess_arr[3], guess_arr[4])

                if (6 - guess_count) > 0 and guess != word:
                    print(f"Remaining guesses: {6 - guess_count}")

    if guess == word:
        pass

    elif guess != word:
        print(f"The word was {word}")

    while True:
        play_again = input("
Would you like to play again? y/n: ")

        if play_again == 'y':
            break

        elif play_again == 'n':
            exit()

        elif play_again == '':
            print("
Error: Please enter a valid answer")

        elif play_again == 'y' and play_again == 'n':
            print("
Error: Please enter a valid answer")
Source by gist.github.com #
 
PREVIOUS NEXT
Tagged: #wordle #python
ADD COMMENT
Topic
Name
5+6 =