Computer Science, asked by abishek08092003, 5 months ago

Write a function in Python to count the number of lines in a text file "HOMEWORK.TEXT" which starts with the alphabet 'M' or 'm'.​

Answers

Answered by allysia
3

USING PYTHON:

The codes are:

____________________________

a=open("HOMEWORK.txt",'r')

b=a.readlines()

count=0

for i in b:

   if i[0]=="M" or "m":

       count+=1

print(count)

a.close()

_______________________________

If you're having any issue viewing it refer to the attachment.

Attachments:
Answered by jai696
2

\large\mathsf\color{pink}{Solution\: using\: python\: 3}

def main():

count_obj = {"m": 0, "M": 0}

with open("HOMEWORK.TEXT") as f:

for line in f:

if line.startswith("m"):

count_obj["m"] += 1

if line.startswith("M"):

count_obj["M"] += 1

return count_obj

for k, v in main().items():

print(f"{k}: {v}")

\large\mathsf\color{lightgreen}useful?\: \color{white}\longrightarrow\: \color{orange}brainliest!

Similar questions