I wrote this code in Python3 which I thought would return a random seven letter word (for a word guessing game) from a text file containing a long list of words. It does find such a word, but returns None. Can anyone explain why ?
from random import randrange
import os
def find_word():
f = open('dictionary.txt')
lines = f.readlines()
x = randrange(1000)
word = lines[x]
word = word.rstrip(os.linesep)
# removes the new line part of text, but the problem is the same with
# or without this line
if len(word) == 7:
print(word, type(word))
return word
else:
find_word()
random_seven_letter_word = find_word()
print(random_seven_letter_word)