I am getting an error saying "print('The person who sent the most emails is:',max_email_sender)" and "NameError: name 'max_email_sender' is not defined" and I know its because of my declarations that are within the try/catch block but im not sure how to rearrange it so the code can work. I tried to declare some of them outside of it but still ran into some problems
try:
file = 'm.txt'
file_open = open(file)
d_dict = dict()
max_email_sender = None
for line in file_open:
line = line.strip()
if line.startswith('From:'):
emails_sent = line[5:].strip()
count_emails = d_dict.get(emails_sent,0)
count_emails += 1
d_dict[emails_sent] = count_emails
if max_email_sender == None:
max_email_sender = emails_sent
elif d_dict[emails_sent] > d_dict[max_email_sender]:
max_email_sender = emails_sent
except (AssertionError, FileNotFoundError) as e:
print(e)
print('The person who sent the most emails is:',max_email_sender)
print('The number of emails sent is:',d_dict[max_email_sender])