Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Write a function ETCount() in Python, which should read each character of a text file “TESTFILE.TXT” and then count and display the count of occurrence of alphabets E and T individually (including small cases e and t too).

Example:

If the file content is as follows:

Today is a pleasant day.

It might rain today.

It is mentioned on weather sites

The ETCount() function should display the output as:

E or e: 6

T or t : 9

 

Answer:

def ETCount ():

    countE= 0

    countT= 0

    f=open( 'TESTFILE.txt','r' )

    lines=f.readlines()

    for line in lines:

        for ch in line:

            if ch== 'E' or ch== 'e' :

                countE=countE+1

            if ch== 'T' or ch== 't' :

                countT=countT+ 1    

    print( "Count of E/e: " ,countE)

    print( "Count of T/t: ", countT)

    f.close()       

 

ETCount()

Question 27(Choice 1) -Teachoo.png

Slide2.JPG

Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 13 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.