I was trying to randomly select a word from a word list. However, since the word list (from file:///usr/share/dict/words; Mac OS) contains almost every word, I wish to ignore those words containing 5 letters or less.
#Getting words from words.txt (retrieved from file:///usr/share/dict/words)
wordList = open(wordBank).readlines()
while True:
wordChosen = random.choice(wordList)
if len(wordchosen) > 5:
break
else:
xxxxxxxx
print wordChosen
How should I code the "else" part where it tells the computer to re-run the random selection until a word with more than 5 letters is found? Can it be performed by using if-else statements?
Aucun commentaire:
Enregistrer un commentaire