Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Anton Kamynin
Anton Kamynin
20,412 Points

Can`t pass the challenge with working solution

Hi! Have some issues with this python challenge. I`ve tested this code on my local python env and everything works fine. But I get an error on submitting this script at treehouse, and have no idea where i can make a mistake. Please, help!)

wordcount.py
def word_count(string):
    result = {}
    string = string.lower().split(" ")
    for word in string:
        if word in result:
            result[word] += 1
        else:
            result[word] = 1
    return result

word_count("I do not like it Sam I Am or Am I not I")

1 Answer

Hi Anton,

The issue here is that you are defining and overriding whitespace. If you use the default whitespace split method, .split(), rather than split(" "), then your code is fine. The first usage splits on any whitespace. Your implementation splits on single spaces only.

I hope that helps,

Steve.

Anton Kamynin
Anton Kamynin
20,412 Points

Great! Thanks a LOT, Steve! It works!

No problem! :+1: