Home > other >  Python tuple wordle guesser <string> error
Python tuple wordle guesser <string> error

Time:02-06

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

CodePudding user response:

You can use map() to determine whether each deadword is in a given word, then use any() to find if there is any deadword present in the word:

list1 = [
"sleep",
"bbbbb"
]

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

for word in list1:
  if not any(map(lambda x: x in word, deadwords)):
    c  = 1
    print(f"found: {word}")

print(c)

CodePudding user response:

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)]
  •  Tags:  
  • Related