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

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

Be sure you are not splitting only on spaces

My code works fine in workspaces. When I try it on the challenge, it gives me the error "make sure you are not splitting only on spaces" Anyone for the rescue?

wordcount.py
# 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(word):
    splitted_word = word.split( )
    dic = {}
    for word in splitted_word:
        dic[word] = splitted_word.count(word)
    return dic

1 Answer

Hi Abdulkadir,

The grader seems to be very picky in this particular case. If it works on your local machine or workspaces, it does not necessarily mean it will pass the grader tests.

Remember that the function named word_count takes a string (of words). Lowercase and split the string and you will be okay.

I hope this helps. If it does, please feel free to mark it as best answer.

def word_count(word): # <-- correction needed here
    splitted_word = word.split( ) # <-- correction needed here
    dic = {}
    for word in splitted_word:
        dic[word] = splitted_word.count(word)
    return dic

I added a string.replace(", " , " ") before splitting with split(), and that worked. For some reason it would not pass by using string.split(" "), even though that worked fine on my own computer, getting the same results. This particular course has been really hard for some reason with the challenges.

Hi there Colby,

There is a hint in the example that was given, that we are supposed to Lowercase the string to make it easier. I think that's what is tripping people up.