Uploaded by muradnali

SimplePythonProgram

advertisement
In [1]:
'''
This program first gets the name of the input file. It then counts the occurren
ce
of each word in the file and prints out the word that occurs the most often toge
ther
with the number of occurrences.
'''
name = input('Enter file:') #input.txt can be used to test this program
handle = open(name)
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print("The word '", bigword, "' occurs the most often and occurs ",
times.", sep='')
Enter file:input.txt
The word 'the' occurs the most often and occurs 5 times.
bigcount, "
Download