Skip to content

Instantly share code, notes, and snippets.

@LiamSwanepoel
Last active May 31, 2023 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiamSwanepoel/7e39a041cfdae162187ed23ec9c0f481 to your computer and use it in GitHub Desktop.
Save LiamSwanepoel/7e39a041cfdae162187ed23ec9c0f481 to your computer and use it in GitHub Desktop.
Program that copies Wordle (https://www.nytimes.com/games/wordle/index.html) but instead made in Python, using the enchant, colorama, and randomwords packages to have the same features as the real game
from random_words import RandomWords
from colorama import Fore
import enchant
print("\nTry to guess the word in 5 tries. After each guess, the colour of the letters will change to show how close "
"\nyour guess was to the word. If the letter is green it is in the word and it is in the correct place. If the "
"\nletter 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("\nPlease enter a 5 letter word: ")).lower()
has_number = False
for char in guess:
has_number = char.isdigit()
if has_number:
print("\nError: 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("\nError: Please enter a 5 letter word")
elif len(guess) < 5:
print("\nError: Please enter a 5 letter word")
elif not words_us.check(guess) and not words_gb.check(guess):
print(f"\n{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 = guess_arr.index(guess_arr[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("\nWould you like to play again? y/n: ")
if play_again == 'y':
break
elif play_again == 'n':
exit()
elif play_again == '':
print("\nError: Please enter a valid answer")
elif play_again == 'y' and play_again == 'n':
print("\nError: Please enter a valid answer")
from random_words import RandomWords
from colorama import Fore
import enchant
print("\nTry to guess the word in 5 tries. After each guess, the colour of the letters will change to show how close "
"\nyour guess was to the word. If the letter is green it is in the word and it is in the correct place. If the "
"\nletter 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("\nPlease enter a 5 letter word: ")).lower()
has_number = False
for char in guess:
has_number = char.isdigit()
if has_number:
print("\nError: 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("\nError: Please enter a 5 letter word")
elif len(guess) < 5:
print("\nError: Please enter a 5 letter word")
elif not words_us.check(guess) and not words_gb.check(guess):
print(f"\n{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("\nWould you like to play again? y/n: ")
if play_again == 'y':
break
elif play_again == 'n':
exit()
elif play_again == '':
print("\nError: Please enter a valid answer")
elif play_again == 'y' and play_again == 'n':
print("\nError: Please enter a valid answer")
@LiamSwanepoel
Copy link
Author

Fixing an issue: if the user enters a word with 2 of the same letter, then check each letter and only set the letter in the correct position to green, if not then set the correct letter in the wrong position to yellow

@LiamSwanepoel
Copy link
Author

Issue has been fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment