Answer to Question #193820 in Python for Coman

Question #193820

Write a program that inputs several(three) lines of text and a search string, and determines the total occurrences of the string in the lines of text.


1
Expert's answer
2021-05-15T17:08:14-0400
def countOccurences(s, word):
    a = s.split(" ")
    count1 = 0
    for i in range(0, len(a)):
        if (word == a[i]):
           count1 = count1 + 1
    return count1  


f = open('my_file.txt', 'r+')
serachWord=input("Enter a word to search: ")
count2=0;
for line in f.readlines():
    count2+=countOccurences(line,serachWord)
print (count2)
    
f.close()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS