# Count the number of words in a text file and # make a dict of all the words in the file word_dict = dict() silly_file = open("silly.txt", "r") for line in silly_file: #print line, word_list = line.split() print word_list for word in word_list: # If word is already a key in word_dict then add one to the # word count that is currently present for that word. # If word is NOT currently a key in word_dict, add an entry for word # with a count of 1. # Note that a new assignment of a value to a key will replace # whatever was there before in the dictionary. if word in word_dict: word_dict[word] = word_dict[word] + 1 else: word_dict[word] = 1 silly_file.close() print word_dict