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

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Word count - works on console but not in challenge

Hi, I've tried putting this through the console, and checked for indentation and misalignment etc. For some reason it doesn't seem to pass, the error says I should make sure to place the string in lowercase and split avoiding whitespaces which I have.

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(string):
    string.lower()
    counter = {}
    for word in string.split():
        try:
            counter[word]+=1
        except KeyError:
            counter[word]=1
    return counter

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Valeshan,

Nice solution. Your logic is correct, the only issue is that by calling string.lower() on its own, you aren't permanently changing the string.

Just reassign it to the string variable and everything will work as it is.

string = string.lower()
Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Thanks! I'm glad it's a simple fix