Fewpal
0 votes
757 views
in Computer by (47.7k points)
closed by

Write a function AMCount() in Python, which should read each character of a text file STORY.TXT, should count and display the occurance of alphabets A and M (including small cases a and m too). 

Example: 

If the file content is as follows: 

Updated information 

As simplified by official websites. 

The EUCount() function should display the output as: 

A or a:4 

M or m :2

1 Answer

+1 vote
by (50.2k points)
selected by
 
Best answer

def count_A_M(): 

f=open("story.txt","r") 

A,M=0,0 

r=f.read() 

for x in r: 

if x[0]=="A" or x[0]=="a" : 

A=A+1 

elif x[0]=="M" or x[0]=="m":

M=M+1

f.close() 

print("A or a: ",A) 

print("M or m: ",M)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...