Search
 
SCRIPT & CODE EXAMPLE
 

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")
Comment

PREVIOUS NEXT
Code Example
Python :: isnumeric 
Python :: python check for duplicate 
Python :: python check if 3 values are equal 
Python :: templateDoesNotExist Django 
Python :: post to instagram from pc python 
Python :: create column for year in dataframe python 
Python :: rotate image in pygame 
Python :: python checking if something is equal to NaN 
Python :: how to clean environment python 
Python :: how to get unique value of all columns in pandas 
Python :: plot size 
Python :: index in list 
Python :: make tkinter label and input 
Python :: tensorflow_version 
Python :: replace string if it contains a substring pandas 
Python :: found features with object datatype 
Python :: change font size in plt 
Python :: opencv invert image 
Python :: dataframe create 
Python :: set value based on column 
Python :: fnd closest element in array numpy 
Python :: joining pandas dataframes 
Python :: read file into list python 
Python :: python program for swapping position of two numbers 
Python :: python write line break 
Python :: /bin/sh: 1: python: not found 
Python :: ejercicios con funciones en python 
Python :: python find the average of a list 
Python :: python line_profiler 
Python :: date.month date time 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =