B04B – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-CBSE12A-2018-B04B #PYTHON#6142    siteicon   siteicon  

Problem Statement - B04B

Write a method in python to read lines from a text file INDIA.TXT, to find and display the occurrence of the word “India”.
For example:
If the content of the file is

“India is the fastest growing economy. 
India is looking for more investments around the globe.
The whole world is looking at India as a great market. 
Most of the Indians can foresee the heights that India is
capable of reaching.”

The output should be 4

Solution

TC++ #6142

def display1():
    c=0
    file=open('INDIA.TXT','r')
    c=0
    for LINE in file:
        Words = LINE.split()
        for W in Words:
            if W=="India":
                c=c+1
        print c
        file.close()
OR
def display():
    c=0
    file=open('INDIA.TXT','r')
    lines = file.read() # lines = file.readline()
    while lines:
       words = lines.split()
       for w in words:
           if w=="India":
               c=c+1
       lines = file.read() # lines = file.readline()
    print c
    file.close()


Share

CSKC| Created: 9-Jan-2019 | Updated: 10-Jan-2019|CBSE12A-2018









Back