Q. Write a function countmy() in Python to read the text file "DATA.TXT" and count the number of times "my" occurs in the file.

For example, if the file "DATA.TXT" contains "This is my website. I have displayed my preferences in the CHOICE section." - the countmy() function should display the output as: "my occurs 2 times".


Answer =

 

def countmy ():
  f = open("DATA.txt", "r")
  count = 0
  x = f.read()
  word = x.split()
  for i in word :
    if (i == "my") :
      count = count + 1
  print ("my occurs", count, "times")
  f.close()


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post