Question: REQUESTING HELP WITH IMPORTING A .TXT FILE TO A PYTHON TO GENERATE RANDOM WORDS this is my current file. i am working out of a
REQUESTING HELP WITH IMPORTING A .TXT FILE TO A PYTHON TO GENERATE RANDOM WORDS
this is my current file. i am working out of a remote github repo in VScode, and windows. when i pull up ubuntu terminal in VS, it puts me in the path /mnt/Users/My_Username
I am trying to import a file provided called words.txt (really just the 1000 word list from MIT)
HERE IS MY CODE AND THANK YOU
"""Word Finder: finds random words from a dictionary."""
import random
class WordFinder:
def __init__(self, "megoshmego/bootcamp_python/words.txt"):
"""Read dictionary and reports number of words"""
dict_file = open("megoshmego/bootcamp_python/words.txt")
self.words = self.parse(dict_file)
print(f"{len(self.words)} words read")
def parse(self, dict_file):
"""Parse dict_file -> list of words"""
return [w.strip() for w in dict_file]
def random(self):
"""Return random word."""
return random.choice(self.words)
PARAMETERS FOR THE PROJECT
Random Word
Youll need to make a class that works like this:
it is instantiated with a path to a file on disk that contains words, one word per line
it reads that file, and makes an attribute of a list of those words
it prints out [num-of-words-read] words read
(it doesnt need to do all of this directly in the __init__ method; it might be a good idea for the __init__ method to call other functions to do some of this.)
it provides a method, random(), which returns a random word from that list of words
Note: the random method should not re-read the list of words each time; it should work with the already-read-in list of words.
For example, assume you have a file at /Users/student/words.txt that looks like this:
cat dog porcupine
Working with your class should work like this:
>>> wf = WordFinder("/Users/student/words.txt") 3 words read >>> wf.random() 'cat' >>> wf.random() 'cat' >>> wf.random() 'porcupine' >>> wf.random() 'dog' Some notes:
Youll need to learn how to read files in Python you can look at the documentation at http://python.org as a good place to start
When Python reads files line-by-line, it still keeps the newline character at the end of each line. Make sure you take that off so that when you find a random word, you return it as cat, not cat
You can make a list of words yourself, or use the words.txt file in the starter code if youre not on Windows, your computer already has a giant list of English words! On OSX, this is at /usr/share/dict/words
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
