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

Why is my code not accepted by the site, when it works in my terminal ?

I am currently running python 3.0 on my laptop and the code is running fine, however the site does not accept my answer.

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

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Konstantin,

A good rule to remember: even if it works on your machine, does not mean that it satisfies the requirements of the challenge and it's code checker.

Here, all you code is syntactically correct :thumbsup: However... you should be splitting on all whitespace not just spaces.
I was positive that at one point, this was explained in the instructions, but not anymore. If you just change your split() method to split on all whitespace, the challenge does pass.

Hope this helps and Great Job so far!! :) :dizzy:

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Ah... if you run your code in the code checker, you will get the error that states "Be sure you're lowercasing the string and splitting on all whitespace!"

I knew I saw the "all whitespace" somewhere. :)

Thank you very much for the quick and valuable feedback. Will pay more attention in future.

As silly as it may seem I had no idea white spaces was different from spaces.