Oct-03-2020, 06:04 AM
@buran
solved at stack-exchange
solved at stack-exchange
# dictionary to store count of each word (2 characters) eg. "an": 2 wordDict = {} file = open("paradise.txt", 'r') # read each line in file for line in file: # read each word in line for word in line.split(): # get only first two letters of word word = word[:2] # If word is not in dictionary then add it if word not in wordDict: wordDict[word] = 1 # else increment the count else: wordDict[word] += 1 file.close() # print all values for key, val in wordDict.items(): print(key, val) # print total print(f"Total: {sum(wordDict.values())}")