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

Pranav Mathur
Pranav Mathur
1,061 Points

Code Challenge: Word Count

I ran this code in my Terminal and works smooth, but does't pass the code challenge?

# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.
def word_count(onestring):
    onestring = onestring.lower()
    onelist = onestring.split(" ")
    onedict = {}
    for word in onelist:
        if word in onedict:
            onedict[word] += 1
            onedict[word] = 1
    return onedict

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there! You are super close here. I know that you've tested it and it looks like it works, so the real problem here is your test data. Keep in mind that the challenge asks you to split on all whitespace. This includes whitespace such as tabs and newline characters. I'd be willing to bet that your test data didn't include tabs or newline characters :smiley:

Currently, you are only splitting on spaces by using split(" "). This tells Python to split on all spaces. Removing the arguments from the split method causes it to split on all whitespace (including tabs and newline characters).

#you have this
split(" ")

#you need this

Hope this helps!