# Reads in file one line at a time and # counts the number of times each word # occurs in the file in_file = "silly.txt" silly_file = open(in_file, "r") word_counts = {} # loop over each line in the file for silly_line in silly_file: # split cuts a string up into a list of smaller strings words = silly_line.split(" ") # loop over each word in the line for word in words: # check if the word is a key in the dictionary already if word in word_counts: # we already saw the word at least once, so add one to the count word_counts[word] = word_counts[word] + 1 else: # we never saw this word before (otherwise the word would be a key # in the dictionary), so add the key to the dictionary and set its # value (the count) to one word_counts[word] = 1 print(word_counts) silly_file.close()