[Python] How do I return value of a dictionary as a string instead of a integer? [closed]

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    Trying to understand what the issue is with the following code:

    mbox-short.txt is a email list and a code checks for the # of messages from iupui.edu. It is supposed to be 8 but the code checker says it is supposed to be '8' instead. How do I modify the code so the checker will confirm this is ok?

    Here is the code:

    with open("mbox-short.txt", "r") as filename:
    message_count = {}
    messages = filename.readlines()

    for message in messages:
    words = message.split()
    #print(words)
    if len(words) > 1 and '@' in words[1] and words[0]=="From":
    email = words[1]
    address = email.split('@')[1]
    if address in message_count:
    message_count[address] += 1
    else:
    message_count[address] = 1
    for address, count in message_count.items():
    print(f"{address}:{count} messages")


    AFTER COMPILING, it gives:

    uct.ac.za:6 messages
    media.berkeley.edu:4 messages
    umich.edu:7 messages
    iupui.edu:8 messages
    caret.cam.ac.uk:1 messages
    gmail.com:1 messages


    error checker complaint:

    Result Actual Value Expected Value Notes
    Fail 8 '8' message_count['iupui.edu']
    Pass 6 6 len(message_count)


    Any idea why? Please see above. Checker wants '8' instead of 8 (see Fail).

    Continue reading...

Compartilhe esta Página