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

George Clement
George Clement
2,816 Points

this works for me but it says the app is getting the wrong output

kinda strange

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(the_phrase_to_parse):
    the_phrase_in_lower = the_phrase_to_parse.lower()
    the_phrase_to_in_list = the_phrase_in_lower.split(" ")

    storage_dic = {}

    for _ in the_phrase_to_in_list:
        if _ in storage_dic:
            storage_dic[_] = storage_dic[_] + 1
        else:
            storage_dic[_] = 1

    return storage_dic

1 Answer

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

Hi there, George Clement ! First, you're doing terrific and you are far from the first to be stumped by this. Would you believe that the problem lies partially between your test data and the interpretation of the instructions? The instructions say you should split on all whitespace. This includes things like tabs and newlines. But if I were a betting woman, I'd bet that your test data didn't contain any tabs or newline characters, so you got back exactly what you would expect. The reason for this is that you are splitting only on spaces.

This splits on spaces:

split(" ")

But this splits on all whitespace:

split()

Simply removing the " " from the arguments in the split() causes this to pass with flying colors!

Hope this helps! :sparkles: