'Python tuple wordle guesser <string> error

So, i'm making a wordle guesser. I can't get the "if (deadwords) not in x:" it's telling me to make it a string but as a string it doesn't function properly (forgets about words that have the letters) I believe its called a tuple, but i'm not sure how to use those...

Code:

list1 = [
lotta words (too many)
]

c = 0
deadwords = ("r","o","s","k","n")

for x in list1:
  if (deadwords) not in x:
    c += 1
    print(f"found: {x}")

print(c)

Error message:

Traceback (most recent call last):
   File "main.py", line 2324, in <module>
      if (deadwords) not in x:
TypeError: 'in <string>' requires string as left operand, not tuple


Solution 1:[1]

I'd recommend using set for this:

word_list = ["hello", "world"]


deadwords = {"r", "o", "s", "k", "n"}
for word in word_list:
    if deadwords.intersection(word):
        print(f"deadword found in {word}")

The deadwords.intersection(word) will return an empty set if none of the letters in deadwords are in the word.

You can alter this code then to narrow down the possible guesses once you've found words to remove. You'd want to maintain a copy of word_list and modify it instead of the original, for example:

word_list = [...]
dead_letters = set()

remaining = word_list.copy()
while not game_over:
    # guess word
    # validate guess
    # add bad letters to the dead_letters set

    remaining = [word for word in remaining if not dead_letters.intersection(word)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1