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

Konstantin Pashkov
Konstantin Pashkov
4,300 Points

every time I submit code I get error but I think code is correct.

I'm submitting my code but get error. I tried it on IDE and received the same result as in example.

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(a):
    new_list = a.lower().split(" ")
    dict1 = {}
    for item in new_list:
        if item in dict1:
            dict1[item]+=1
        else:
            dict1[item]=1
    return dict1

1 Answer

Hey, here is something quick I wrote that passes

def word_count(words):
    word_dict = {}
    words = words.lower().split()
    for word in words:
        if not word_dict.get(word):
            word_dict[word] = 1
        else:
            word_dict.update({word : word_dict.get(word) + 1})
    return word_dict

I'm now looking at yours and seeing we basically did the same thing, however, I see your problem. You are splitting only on spaces, not white spaces in general, so a string like "I do not like it Sam I Am" will fail. Remove the argument from the split method like I did and I believe yours should pass.