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

I run this in my IDLE with no problem

def word_count(words): newdic = {} words = words.lower() words = words.split(" ") for word in words: try: newdic[word]+=1 except KeyError: newdic[word]=1

return newdic
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(words):
    newdic = {}
    words = words.lower()
    words = words.split(" ")
    for word in words:
        try:
            newdic[word]+=1
        except KeyError:
            newdic[word]=1

    return newdic    

2 Answers

Don't worry, everyone gets caught out on this one, the checker is SUPER strict. I even went as far as stripping all not alpha, verifying the split string wasn't empty after filtering lol...

It's the .split(" ") that's causing the problem. As far as I can tell they're scanning the syntax you wrote to see if you used it instead of the default .split(), not just comparing output. My code filtered out everything that using split(" ") would allow through and it still failed lol. So you can provide the correct output to all their tests and still fail if you used split(" ").

The reason I assume is just to remind you that you don't add an argument to split() if you want all whitespace removed, only if you specifically want it split at certain points.

thank you for encourage.