0

I'm trying to solve the assignments from the Python Coursera Course Chapter 9.4.

Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

name = input("Enter file:")
if len(name) < 1:
    name = "mbox-short.txt"
handle = open(name)

emails = dict()
addresses = list()

for lines in handle:
    line = lines.split(':')
    if line[0] == 'From':
        addresses.append(line[1])
        
for address in addresses:
    emails[address] = emails.get(address, 0) + 1

bigcount = None
bigemail = None

for address,count in emails.items():
    if bigcount is None or count > bigcount:
        bigemail = address
        bigcount = count
        
print(bigemail,bigcount)

That is my code so far, it runs and the output is:

 cwen@iupui.edu
 5

instead of the correct answer which is:

cwen@iupui.edu 5

I have tried doing lines.rstrip() after the first for loop to get rid of spaces which then printed the two variables on the same line but added a space at the front and end, causing another mismatch from the desired output.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    You just need to strip `line[1]`, the address. – wjandrea May 21 '23 at 17:55
  • 1
    Please take the [tour] and read [ask]. SO is a Q&A site, but there wasn't a question here until I edited one in. There are also more tips, like making a [mre] and how to write a good title. The one I put is better, but ideally it would be more specific, which'd require more legwork on your part, trying different things and checking where in your code the unwanted spaces show up. See also [How to ask and answer homework questions](//meta.stackoverflow.com/q/334822). You might also want to read [How to step through Python code to help debug issues?](/q/4929251). – wjandrea May 21 '23 at 18:03

0 Answers0