I'm having trouble with creating a function that would generate a random word from a list of four-Letter words from a file called fiveLetterWords.txt. I know how to generate the list and print it but I can't seem to get the random part right. I have attached the fiveLetterWords.txt file at the bottom of this post. The point of creating this program is to generate a random word when the user types import randomWord and then secretWord = randomWord.getRandomFiveLetterWord().
import random
def getRandomFiveLetterWord(e):
file = open(e, "r")
t = file.read()
randomWord = random.randint(file[0], file[-1])
file.close()
return randomWord
print "The Five-Letter Word Generator generates:", getRandomFiveLetterWord("fiveLetterWords.txt")
Traceback (most recent call last):
File "H:/me/fiveLetterWordGenerator.py", line 8, in <module>
print getRandomFiveLetterWord("fiveLetterWords.txt")
File "H:/me/fiveLetterWordGenerator.py", line 5, in getRandomFiveLetterWord
randomWord = random.randint(file[0], file[-1])
TypeError: 'file' object is unsubscriptable