Any idea how to filter out words based on wether or not they have letters?

Question:
Hi! So, I’m making a code to solve the NYT Spelling Bee Game, and I’m struggling to filter out words that have letters other than the provided ones. I’ve briefly annotated the problem section, and everything else should be working fine. Thanks!

Repl link:
https://replit.com/@kylieo141/Spelling-bee

Code Snippit:

 for count in range(len(oldtrue)-2):
   word.append(oldtrue[0])
   test=word[0]
   
   for d in range(len(test)):
     temp=test[d]
     temp=str(temp)
 
     if temp == other[0]:
       bad=False
     elif temp == other[1]:
       bad=False
     elif temp == other[2]:
       bad=False
     elif temp == other[3]:
       bad=False
     elif temp == other[4]:
       bad=False
     elif temp == other[5]:
       bad=False
     elif temp == other[6]:
       bad=False
     else:
       bad=True

     if bad==True:
       count+=1
       break
     else:
       bad=True
 
   if count >= 1:
     bad=True
   else:
     final.append(test)
  
   word.pop(0)
   oldtrue.pop(0)

:wave: Welcome @kylieo141!

Which letters are the provided ones? You could do something like this:

for word in word_list:
    for letter in word:
        if letter not in provided_letters:
            bad = True

And of course, change the variable names.

Also, the code snippet you provided can be improved to be more D.R.Y.

for count in range(len(oldtrue)-2):
   word.append(oldtrue[0])
   test=word[0]
   
   for d in range(len(test)):
     temp=test[d]
     temp=str(temp)

     bad = False
 
     if temp not in other:
       bad=True

     if bad:
       count+=1
       break
 
   if count >= 1:
     bad=True
   else:
     final.append(test)
  
   word.pop(0)
   oldtrue.pop(0)
1 Like