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

Daniel Fragoso
Daniel Fragoso
5,333 Points

[SOLVED] Difficulties on the word count exercise in the dictionaries section of Python Collections.

I'm getting an error message telling me to make sure I'm splitting on all whitespace, which makes me wonder if I need to split on '\n' as well as ' '. If so, I'm not sure how to do that. Otherwise, my code seems to be getting the job done on tests I've done on my computer, but I'm still getting the error message.

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(s):
    l=s.lower().split('\n')
    d={}
    for entry in l:
        try:
            d[entry]+=1
        except KeyError:
            d[entry]=1
    return d
Daniel Fragoso
Daniel Fragoso
5,333 Points

Sorry about that random e in there btw, I'm not sure how it got there!

Daniel Fragoso
Daniel Fragoso
5,333 Points

Nevermind, I figured it out! Turns out you do need to split on all whitespace, and you do that by leaving the argument on the .split method empty.