Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Han Li
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Han Li
Python Web Development Techdegree Graduate 14,817 Points

Word Count

Hi,

I got the following error in the challenge task.

Bummer: Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!

Please help me!

Thanks, Ross

wordcount.py
def word_count(string):
    word_count_dict = {}
    for word in string.lower().split(" "):
        try:
            word_count_dict[word] += 1
        except KeyError:
            word_count_dict[word] = 1
    return word_count_dict

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,624 Points

Hi Ross Peace

Your syntax is correct.
Now, the clue to the problem is in the error you received. It says to make sure you are "splitting on all whitespace", but your code is splitting on only spaces (new lines, tabs, etc are also whitespace).
Remember, to split on all whitespace, you don't pass any parameters into the method.

Once you fix that up, the rest is great.

Nice work! :) :dizzy: