Fix a potential infinite loop.

If the word file has fewer words than the number we're looking for,
we use'em all.
This commit is contained in:
Gonçalo S. Martins 2016-05-17 21:46:07 +01:00
parent 3747c8a98c
commit 7cc9205fbf

View file

@ -42,13 +42,17 @@ def read_word_list(filename, n_words=100):
for line in words_file:
words.append(line.strip())
# Select n_words words
while len(selected_words) < n_words:
# Choose candidate randomly
candidate = words[random.randint(0, len(words)-1)]
# Append candidate if it's not already there
if candidate not in selected_words:
selected_words.append(candidate)
# If we have few words, we use them all
if len(words) <= n_words:
selected_words = words
# If we have enough words, we have to select
else:
while len(selected_words) < n_words:
# Choose candidate randomly
candidate = words[random.randint(0, len(words)-1)]
# Append candidate if it's not already there
if candidate not in selected_words:
selected_words.append(candidate)
# ... and return the list
return selected_words