Q. Write a function in Python to count the number of lines in a text file 'STORY.TXT' which are starting with the alphabet 'A'.
Answer =
def COUNTLINES () :
file = open ('STORY.TXT', 'r')
lines = file.readlines ()
count=0
for w in lines :
if w[0] == "A" or w[0] == "a" :
count = count + 1
print ("Total lines : ", count)
file.close()
The "print ("Total lines : ", count)" command should be outside the for loop.
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )