Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.