In: Computer Science
Assume a file containing a series of words is named words.txt and exists on the computer’s disk. Write a program that reads the data and counts the number of occurrences of each letter. The program should then displays how many times each letter appears in the sentence.
If you have any doubts, please give me comment...
occ = {}
try:
fp = open("words.txt")
for line in fp.readlines():
for ch in line.strip():
if ch.isalpha():
if ch not in occ:
occ[ch] = 0
occ[ch] += 1
fp.close()
for ch in occ:
print(ch+" : "+str(occ[ch]))
except FileNotFoundError:
print("Unable to open words.txt")
