I'm not sure why I keep the wrong answer. I first found the days through for line in hand loop and then added key and value through dictionary, yet I don't get the output that I thought about. Could you please help me? than you!
Exercise 2: Write a program that categorizes each mail message by which day of the week the commit was done. To do this look for lines that start with “From”, then look for the third word and keep a running count of each of the days of the week. At the end of the program print out the contents of your dictionary (order does not matter).
Sample Line:
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Sample Execution:
python dow.py
Enter a file name: mbox-short.txt
{'Fri': 20, 'Thu': 6, 'Sat': 1}
Here is my code
#mbox-short.txt
fname = input("Enter a file name: ")
hand = open(fname)
counts = dict()
for line in hand:
line = line.rstrip()
if not line.startswith('From '): continue
words = line.split()
words = words[2]
for word in words:
if word not in counts:
counts[word] = 1
else:
counts[word] += 1
print(counts)
output
{'S': 1, 'a': 1, 't': 1, 'F': 20, 'r': 20, 'i': 20, 'T': 6, 'h': 6, 'u': 6}