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

Ariful Islam
Ariful Islam
1,781 Points

Getting result as expected in my local shell but treehouse says wrong

Getting result as expected in my local shell but treehouse says wrong. Please check the attached file.

# 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(arg):
    words = arg.split(" ")
    dic = {}
    for word in words:
        if word.lower() in dic:
        dic[word.lower()] = words.count(word)

    return dic

1 Answer

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hi Ariful,

You've pretty much have it perfect, except for one small thing. If you think about how someone might input the argument... you can't be sure that it will be a simple string separated by spaces. The argument passed in could contain a list separated with hard-returns or something spaced out with tabs. When you did it in the shell, it was just a string with spaces (I'm guessing), so the output would be what you want.

Instead of splitting on just spaces, you should be splitting on all whitespace. If you just change that up, the challenge should pass. Remember to split on all whitespace, you don't pass any argument into the split() method.

Nice work!! :) :dizzy:

Ariful Islam
Ariful Islam
1,781 Points

Thank you so much @Jason.