findWord.py (40 points: string manipulation) This program calls a function (find
ID: 3729195 • Letter: F
Question
findWord.py (40 points: string manipulation) This program calls a function (findlt) that manipulates strings The function is run over and over again until the user enters specific outputs to quit. findIt (lines, word) takes in a group of strings stored together (that is, lines in a list) in an EXTERNAL FILE called "theRaven.txt", which contains the entire poem, "The Raven", by Edgar Allan Poe. It then goes through each string (line) in this list and searches for the sub- string "word". If it finds "word", it prints the entire line, preceded by the line number (these start at 0). See example below The line number is the position of the line in the list. So for example, if the list has 125 lines in it and the one indexed number 103 is "found" by the function and is "Quoth the Raven Nevermore", then the function would output 103 Quoth the Raven "Nevermore." The reading of the external file is done for you in the main findWord.py program HINTS: When a list contains multiple values, you can access each individual one with a for- loop, for example, given a list called people = "joe""mary "bob"chincilla" ], you would do this >>>for one in people: print(one)Explanation / Answer
def FindWord(lines, word): for i, line in enumerate(lines): if word in line: print(str(i) + ' ' + line.strip()) def main(): MyFile = open("theRaven.txt") lines = MyFile.readlines() word = input("Enter word to find: ") if word != "": FindWord(lines, word) MyFile.close() if __name__ == '__main__': main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.