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
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
2
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}")
Similar questions