Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,120 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!