我正在完成一个练习,要求我创建一个dict()
,并找出谁发送了最多的电子邮件。正确的输出应该是cwen@iupui.edu 5
,但我的代码返回None None
。这是我的密码:
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
if not line.startswith('From '):
continue
recipient = line.split()
email = recipient[1]
counts[email] = counts.get(email,0) + 1
maxcount = None
maxemail = None
for key,value in counts.items():
if maxcount is None or value> maxcount:
maxemail = key
maxcount = value
print(maxemail, maxcount)
我知道我的输出返回None None
,因为我的字典中没有任何内容,所以我的代码哪里出错了?我试图在for循环中移动一些行,但收到了相同的输出
“continue”(继续)下面的三行不应包含在内。也就是说,应该与前面的“if”行处于同一级别
相关问题 更多 >
编程相关推荐