Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help on python mycode: file=open(\"search1.txt\",\"r+\") word1=open(\"word1

ID: 3914764 • Letter: N

Question

Need help on python

mycode:

file=open("search1.txt","r+")
word1=open("word1.txt","r+")
wordcount={}
word2=word1.readlines()
for word2 in file.read().split():
if word2 not in wordcount:
wordcount[word2] = 0
else:
wordcount[word2] += 1
print (word2)
file.close();

word 1:

Tears
go
I
you
Love
when

search1.txt :

What do I do,
When I'm still in love with you?
You walked away,
'Cause you didn't want to stay.
You broke my heart, you tore me apart.
Every day I wait for you,
Telling myself our love was true.
But when you don't show, more tears start to flow.
That's when I know
I have to let go.

Explanation / Answer

file = open("search1.txt", "r+") word1 = open("word1.txt", "r+") wordcount = {} line_number = 1 for line in file: words = line.strip().split() for word in words: word = word.lower() if word[-1] == '.' or word[-1] == ',' or word[-1] == '?': word = word[:-1] if word not in wordcount: wordcount[word] = [word] wordcount[word].append(line_number) line_number += 1 result = [] for line in word1: word = line.strip().lower() if word in wordcount: result.append(wordcount[word]) print(result)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote