Python Python Collections (2016, retired 2019) Dictionaries Word Count

Fernando Santos
Fernando Santos
11,263 Points

Code running on workspace but not on browser

The code is running as required on my workspace, but they don't accept it in the code challenge. I can't realize what's wrong.
def word_count(string):
    lista = string.lower().split(" ")
    lista2 = lista.copy()
    dicionario = {}
    for word in lista:
        count = 0
        while word in lista2:
            count += 1
            dicionario[word] = count
    return dicionario

1 Answer

Steven Parker
Steven Parker
200,782 Points

You're really close! And there's a clue in the error message you get: "Bummer: ... Be sure you're lowercasing the string and splitting on all whitespace!"

To split on "all whitespace" the argument to "split" should be left empty (or set to None). Providing a space argument causes it to split only on individual space characters. See the Python documentation for "split" for more details.