I have a list of words, and a function that searches for a word by its length and the letters inside the word.
The for loop at the end cycles through the list of possible words (generated by the given length), and it should either print the word if it contains the given letter, or delete the word from the list of possible words if it does not contain the given letter.
How can I achieve this here:
wlist = ['Apple', 'Banana', 'Cherry', 'Donkey']
def wordSearch(wlist):
posWords = []
length = int(input('Enter length of word: '))
print("POSSIBLE WORD(S):")
for word in wlist:
stripWord = word.replace(' ', '')
if len(stripWord) == length:
print("%s (%s)" % (word, length))
posWords.append(word)
while True:
searchOptions = ['Add Known Letter', 'Exit']
searchIndex, item = chooseFromMenu(searchOptions)
if searchIndex == 0:
letter = input('Enter letter: ')
print("POSSIBLE WORD(S):")
for word in posWords:
if letter in word:
print(word)
else:
# remove word from list somehow
else:
break
Specifically this block:
letter = input('Enter letter: ')
print("POSSIBLE WORD(S):")
for word in posWords:
if letter in word:
print(word)
else:
# remove word from list somehow
I've tried to do del word
and posWords.pop(word)
but neither seem to work.
I also tried .remove()
but I cant exactly remember what I did with that.
I'll go into more detail with the problem I'm having here...
If I type in the letter 'e', it should remove 'Banana' from the list of possible words so that when I type 'n' I should only get 'Donkey', not 'Donkey' and 'Banana'.
If that makes sense.
I have just tried posWords.remove(word)
and it hasn't done this either...