Exercise

Let's do an exercise.

See the given file, batman.txt, containing the quotes from famous Hollywood movies.

Our aim is to write a program to find a given word from the file:

Screenshot of batman.txt

Let's write a program to find the particular word from the file. We are making the assumption that the program should be case insensitive, which means it does not matter whether the characters are in uppercase or lowercase.

See the code in findword.py:

word = raw_input("Enter the word ")
word = word.lower()
file_txt = open("batman.txt", "r")
count = 0
for each in file_txt:
if word in each.lower():
count = count+1
print "The ", word ," occured ",count, " times"

The program is very easy to understand. Let's see the output of the code:

Output of findword.py

The program findword.py is working fine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.89.18