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

Travis John Villanueva
Travis John Villanueva
5,052 Points

word_count.py

So, i tested in worskpace, and i replicate the example and it returns the same result. I dont know what is wrong as the error message says try again. Can you please shed light?

Thanks!

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(text):
    dict_text = {}
    text_splitted = text.split(" ")
    for i in range(len(text_splitted)):
        text_splitted[i]=text_splitted[i].lower()
    for i in range(len(text_splitted)):
        num_ins = 0
        for a in range(len(text_splitted)):
            if (text_splitted[i] == text_splitted[a]):
                num_ins += 1
        else:
            pass
    dict_text[text_splitted[i]]=num_ins
return dict_text

1 Answer

Steven Parker
Steven Parker
229,645 Points

You're pretty close, but I can see two issues:

First, there's an indentation error on the last two lines. They both need to be indented one more level, otherwise the assignment is outside of the loop and the "return" is completely outside of the function.

Then, when splitting the words, you should split on "all whitespace". To do this, leave the argument to "split" empty. Giving it a space argument causes it to split on individual spaces.

Fix those and you should pass!