I'm recreating Wordle in Python, but I'm running in to a problem. It highlights letters yellow even if there are too many of them. For example, if the word is gas and you enter a word such as gss, it highlights both letters s even though there is only 1 in the word. Here is my code:
import random
import time
import sys
import os
if os.name == 'nt':
    from ctypes import windll
    k = windll.kernel32
    k.SetConsoleMode(k.GetStdHandle(-11), 7)
keys = {'a': 'NSr', 'b': 'rlK', 'c': 'yDD', 'd': 'YBr', 'e': 'XBB', 'f': 'LLo', 'g': 'gZn', 'h': 'LTd', 'i': 'hKn', 'j': 'fWj', 'k': 'dgu', 'l': 'nFN', 'm': 'nNy',
        'n': 'QKD', 'o': 'cJJ', 'p': 'MEA', 'q': 'WTJ', 'r': 'nnM', 's': 'Tru', 't': 'xcE', 'u': 'Msx', 'v': 'Cef', 'w': 'Hkf', 'x': 'obn', 'y': 'myp', 'z': 'PUE'}
keyr = {v: k for k, v in keys.items()}
def encrypt(text):
  if len(text) > 1:
    string = ""
    for char in text:
      if char in keys:
        string += keys[char] + ","
      else:
        return "Only letters are allowed"
        break
    return string
  else:
    return "Text must have something in it"
def decrypt(text):
  text = text[:-1].split(",")
  if len(text) > 1:
    string = ""
    for char in text:
      if char in keyr:
        string += keyr[char]
      else:
        return "Only letters are allowed"
        break
    return string
  else:
    return "Text must have something in it"
print("Welcome to Wordle!")
print("Random or Custom Word?")
ch = input("Type 'r' or 'c' ")
if ch not in ("r", "c"):
  while ch not in ("r", "c"):
    ch = input("Type 'r' or 'c' ")
green = "\u001b[32m"
yellow = "\u001b[33m"
reset = "\u001b[0m"
if ch == "r":
  letters = "abcdefghijklmnopqrstuvwxyz"
  ln = {}
  for char in letters:
    ln[char] = 0
  words = open("words.txt", "r")
  wordl = []
  for item in words.readlines():
    wordl.append(item.strip())
  word = wordl[random.randint(0, 5756)]
  print(f'Your word is 5 letters. Start guessing!')
  num = 1
  correct = False
  while num < 6:
    guess = input(f'Guess {str(num)}: ').lower()
    sys.stdout.write("\033[F")
    invalid = False
    for char in guess:
      if char not in keys:
        print(" " * 250)
        sys.stdout.write("\033[F")
        print("Must be only letters!")
        invalid = True
    if len(guess) > 5:
      print(" " * 250)
      sys.stdout.write("\033[F")
      print("Word is too long.")
    elif len(guess) < 5:
      print(" " * 250)
      sys.stdout.write("\033[F")
      print("Word is too short.")
    elif guess not in wordl:
      print(" " * 250)
      sys.stdout.write("\033[F")
      print("Invalid word.")
    elif invalid:
      pass
    else:
      if guess == word:
        print("Your word is correct!")
        correct = True
        break
      chn = 0
      colored = ""
      for char in guess:
        if char in word:
          if char == word[chn]:
            colored += f'{green}{char}{reset}'
          else:
            colored += f'{yellow}{char}{reset}'
        else:
          colored += char
        chn += 1
      print(f'Guess {str(num)}: ' + colored)
      num += 1
  if correct == False:
    print(f'You lose! The word was {word}. Better luck next time!')
  else:
    print("Congratulations! You win!")
  time.sleep(999)
The solution I tried was creating a dictionary of every letter and the number of times it appears that resets every word you enter, like this:
letters_n = {}
letters = "abcdefghijklmnopqrstuvwxyz"
for char in letters:
  letters_n[char] = 0
And then when it went over a letter, I added the number and checked it against the count of that character in the word:
for char in guess:
  if char in word:
    if char == word[chn]:
      colored += f'{green}{char}{reset}'
    else:
      if letters_n[char] >= word.count(char):
        colored += char
      else:
        colored += f'{yellow}{char}{reset}'
      letters_n[char] += 1
  else:
    colored += char
It still highlighted letters yellow that were not in the word. How can I fix this? (Sorry for long code sample)
 
    