implement the DictWordsRead class which creates a file object from which content can be read from a
Question:
implement the DictWordsRead class which creates a file object from which content can be read from a text file. This class is responsible for reading file's contents, adding words that are of a min length and max length to a list which is then used to be returned by the guessing game.
3 methods are to be defined in this class.
def __init__(self, file, min, max):
file - specifies name of file that contains words to be used by the guessing game
min - minimum length of word that will be selected from ile
max - maximum length of word to be selected from file.
important -
*** if the argument forminis less than 5,minshould be set to 5.
Eg:word_read = DictWordRead(\"name.txt\", 3, 6).
outputs - The min word length is 5 or more. Min has been set to 5
*** if argument formaxis less than that formin, max should be set to min.
eg:word_read = DictWordRead(\"name.txt\", 6 5).
outputs -Max word length should be more than or equal to min word length. It is set to 6.
Next method:
def object_file(self):
returns a file object that is used to read contents of text file specified byfileparameter in the initialiser. Text file contains 0 or more words separated by new line character.
Also use try..except FileNotFoundError if file is not found.
The method asks the user to input a valid filename. While user inputs invalid filename, the user is asked to re-enter a file name or enter \"X\" to exit.
if valid filename is entered, a file object enabling content to be read from this file should be returned. If user enters \"X\" , this method should return None.
Eg:
hello.txt cannot be found.
Please enter a filename or X to stop:xyz.txt
xyz.txt cannot be found.
Please enter a filename or X to stop:contents.txt[valid file]
Third method:
def get_list_words(self, wordfile):
wordfile- file object that can be used to read content from text file with words to be used in the guessing game.
This method should read contents of file, and add words to a list that fall within the range specified by theminandmaxparameters (both min and max inclusive). Eg ifminis 4 andmaxis 7, words of length 4-7 letters are added. Method should return the list.
TEST:
from DictWordRead import DictWordReadword_read = DictWordRead(\"Words2.txt\", 3, 6)print(word_read.min, word_read.max)OUTPUT:
The minimum word length should be 5 or more.It has been set to 5.5 6TEST:
from DictWordRead import DictWordReadword_read = DictWordRead(\"Words2.txt\", 6, 6)print(word_read.min, word_read.max)word_file = word_read.object_file()words = word_read.get_list_words(word_file)print(words)OUTPUT:
6 6['absent', 'bridge', 'cinema', 'danger', 'gadget', 'safari', 'useful', 'vacuum', 'weapon', 'zealot', 'zombie']Sample text file contents:
https://drive.google.com/file/d/1mx8T3fomOuelttc_lpRy8m5d6x7dQWQQ/view?usp=sharing