I am writing a game where there are two losing conditions:
- Forming a word longer than 3 letters. Bee is okay, Beer is not.
- Forming a word that can't be made into a longer word. Zebra is okay, Zebras is not.
Wordlist is a list of words, frag is the previous fragment and a is the new letter a player enters. so frag may look like 'app' and a maybe 'l' with the idea of forming the word apple.
def getLoser(frag, a, wordlist):
word = frag + a
if len(word) > 3:
if word in wordlist:
print 'word in wordlist'
return True
else:
for words in wordlist:
if words[:len(word)] == word:
print words,':', word
print 'valid word left'
return False
else:
print words[:len(word)]
print words,':', word
print 'false found'
return True
else:
return False
For some reason when I enter my 4th letter, it automatically goes to the else in the for loop, even when the if statement functions in the for loop works correctly when I test it alone on dummy data in the interactive trail.
Here is output for the frag alg and the letter e with the word algebra in the wordlist.
e
aa
aa : alge
false found
True
Any ideas?