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

james malloy
james malloy
7,959 Points

word_count function not working - unclear as to why

the returned message say I should be sure I'm not splitting only on spaces, but as I'm not specifying a separator in the split method, i didn't think I was
Any ideas what I'm doing wrong ?

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(input_string):
    ret_dict = {}

    word_list = input_string.lower().split()

    for word in word_list:
        if word in ret_dict:
            ret_dict[word] += 1
        else:
            ret_dict[word] = 1

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

You have done all of the hard work but then forgotten to return anything!

Just return your ret_dict and it's good to go.

james malloy
james malloy
7,959 Points

ha ha, oops! Well spotted.