简体   繁体   English

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

[英]Why is my output returning “None None” when creating a dict()?

I'm completing an exercise that requires me to create a dict() and figure out who has sent the greatest number of email messages.我正在完成一个练习,需要我创建一个dict()并找出谁发送了最多的 email 消息。 The correct output is supposed to be cwen@iupui.edu 5 but my code is returning None None .正确的 output 应该是cwen@iupui.edu 5但我的代码返回None None Here is my code:这是我的代码:

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)

I understand that my output is returning None None because there's nothing in my dictionary, so where is my code going wrong?我知道我的 output 正在返回None None因为我的字典中没有任何内容,那么我的代码哪里出错了? I've attempted to move around some lines within the for loop but received the same output.我试图在 for 循环中移动一些行,但收到了相同的 output。

The three lines below "continue" should be unindented. “继续”下面的三行应该不缩进。 That is, should be at the same level as the "if" line before.也就是说,应该与之前的“if”行处于同一级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM