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 (Retired) Dictionaries Word Count

Where is 'word count'?, how am I not defining the function correctly such that it fails to be read as a funtion.

Where am I missing it?

word_count.py
def word_count(string):
    value = string.lower().split(' ')
    for word in value:
        my_dict[word] = value.count(word)
    return my_dict

2 Answers

Steven Parker
Steven Parker
229,670 Points

:point_right: It looks like you forgot to initialize my_dict before assigning to it.

Initialization to an empty set will identify it as a dictionary and not an array.

Thanks a lot.

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

You are so very close!

def word_count(string):
    my_dict = {}
    value = string.lower().split(' ')
    for word in value:
        my_dict[word] = value.count(word)
    return my_dict