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

Every time the same error. Working on computer but not here. What's wrong?

Bummer! Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!

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


# Lowercase the string to make it easier.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Oh wow... you're so close here! The key here lies in the fact that you need to split on all whitespace. This includes tabs and new lines. Currently, you're only splitting on spaces.

To split on all whitespace, simply leave the the argument in the split method blank. Take a look at an example:

myString.split()

If I make this tiny correction in your code, it passes with flying colors!

Good luck and keep coding! :sparkles: