为什么我的输出在创建dict()时返回“None None”?

2024-10-01 02:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在完成一个练习,要求我创建一个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循环中移动一些行,但收到了相同的输出


Tags: 代码nameinnoneforifvalueemail