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

Payam Mesgari
seal-mask
.a{fill-rule:evenodd;}techdegree
Payam Mesgari
Python Web Development Techdegree Student 1,072 Points

I don't get it why the code returns the correct response for different inputs in the IDE and not here!

The return value is a dictionary and the count of the words are correct, why isn't it passing the challenge?

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(mystr):
    mystr = mystr.lower()
    split_str = mystr.split(' ')
    c = 1
    mydict = {}
    for word in split_str:
        if word in mydict.keys():
            c += 1
            mydict[word] = c
        else:
            c = 1
            mydict[word] = c
    return mydict

4 Answers

Evan Trimby
Evan Trimby
5,381 Points

I was splitting the same way so thanks for the info james south. Payam Mesgari Your problem is the variable you are using to add the count. It will never go above 2 the way you have it setup. I would recommend doing away with it and using mydict[word] += 1 .

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

the error tells you to split on all whitespace, but you are splitting on the space character only. there are other whitespace characters such as tab. remove the argument to split to expand what is split on.

Payam Mesgari
seal-mask
.a{fill-rule:evenodd;}techdegree
Payam Mesgari
Python Web Development Techdegree Student 1,072 Points

Evan Trimby thanks! I already left a feedback on slack, the level of details on errors when a challenge fails is very low in Treehouse, and that is a bummer!