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

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,860 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: