I am trying to create a wordle clone in python but it is not working. The problem is that the colours are not showing for some particular reason. Here is my code:
from colorama import Fore,Back,Style
import os
import random
import time
import sys
from typing import Final
WORD_LENGTH: Final = 5
GAME_LENGTH: Final = 6
ANSWERS_FILE_PATH: Final = r"Miscellaneous\Wordle\allWordleAnswers.txt"
ALLOWED_INPUT_WORDS_FILE_PATH: Final = r"Miscellaneous\Wordle\allWordleWords.txt"
def generateWord() -> str:
with open(ANSWERS_FILE_PATH, "rt") as f:
words = [word.strip() for word in f.readlines()]
return random.choice(words)
def getInput(previousWords, word) -> str:
with open(ALLOWED_INPUT_WORDS_FILE_PATH, "rt") as f:
words = [word.strip() for word in f.readlines()]
while True:
inputted = input("What is going to be your next guess: ").lower()
if len(inputted) != WORD_LENGTH:
print("Your guess must be exactly 5 letters long.")
continue
if inputted not in words:
os.system("cls" if os.name == "nt" else "clear")
printWords(previousWords, word)
print("Your guess was not an actual 5-letter word. Please try again.")
else:
os.system("cls" if os.name == "nt" else "clear")
return inputted
def instructions():
print("Wordle\n------")
time.sleep(2.5)
os.system("cls" if os.name == "nt" else "clear")
print("White text means that letter does not appear in the correct word.\n")
os.system("pause")
os.system("cls" if os.name == "nt" else "clear")
print(f"{Fore.YELLOW}Yellow text means that letter does appear in the correct word but in another spot.\n{Style.RESET_ALL}")
os.system("pause")
os.system("cls" if os.name == "nt" else "clear")
print(f"{Fore.GREEN}Green text means that letter does appear in the correct word and that letter is also in the correct spot.\n{Style.RESET_ALL}")
os.system("pause")
os.system("cls" if os.name == "nt" else "clear")
def findColor(input_str, answer_str):
if len(input_str) != WORD_LENGTH or len(answer_str) != WORD_LENGTH:
raise ValueError("Both input and answer must be 5-letter strings")
colors = []
input_used = [False] * WORD_LENGTH
answer_used = [False] * WORD_LENGTH
for i in range(WORD_LENGTH):
if input_str[i] == answer_str[i]:
colors.append(Fore.GREEN)
input_used[i] = True
answer_used[i] = True
else:
colors.append(None)
for i in range(WORD_LENGTH):
if not input_used[i]:
for j in range(WORD_LENGTH):
if not answer_used[j] and input_str[i] == answer_str[j]:
colors[i] = Fore.WHITE
answer_used[j] = True
break
for i in range(WORD_LENGTH):
if colors[i] == None:
colors[i] = Fore.WHITE
return colors
def printWords(toPrintWords: list,comparer: str):
for index,word in enumerate(toPrintWords):
colors: list = findColor(word,comparer)
print(Back.BLACK)
for color,letter in zip(colors,word): print(f"{color}{letter}",end = "")
print(Style.RESET_ALL)
if index == len(toPrintWords)-1: print("")
def main():
os.system("cls" if os.name == "nt" else "clear")
word: str = generateWord()
previousWords: list = []
instructions()
while True:
inputtedWord: str = getInput(previousWords,word)
previousWords.append(inputtedWord)
printWords(previousWords,word)
if inputtedWord == word:
print(f"You won! You guessed the word \"{word}\" in {len(previousWords)} guesses!")
sys.exit()
elif len(previousWords) == GAME_LENGTH:
print(f"You lost. You couldn't guess the word \"{word}\" in time.")
sys.exit()
if __name__ == "__main__":
main()
It is supposed to use colorama's fore, back and style to change the colours of the text but for some unknown reason it doesn't apply it.