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

Python Collections - wordcount.py challenge not accepting my answer (but it works!)

When I tried this with the sample string provided in the comments I get the exact same results as the second line of comments. It's in a slightly different order, which is expected since it's a dictionary and so unordered, however the results the correct. I keep getting a "bummer" error reminding me to lower case everything and split on white spaces (which my code has always done).

I'm not sure what result is expected here if my function isn't producing it as to me nothing looks wrong. I tested it in the REPL and it worked fine, help!

# 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(my_string):
    my_string = my_string.lower()
    my_words = my_string.split(" ")
    my_dict = {}

    for words in my_words:
        my_dict[words] = my_words.count(words)

    return my_dict

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Adam Kingsley ! I received your request for assistance. Yes, it does work, but only sometimes. The key here is in the Bummer! where it asks you to split on all whitespace. Currently, you are only splitting on spaces, but there are also tabs and new line characters to consider as well. Your test data likely doesn't have any of the latter so you are getting back a false positive.

This splits on spaces:

.split(" ")

But this splits on all whitespace:


Simply removing the quotation marks as arguments from your split method causes this to pass with flying colors! :sparkles:

Thank you, that makes a lot of sense!