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

Evgeny Shilov
Evgeny Shilov
21,283 Points

Help with the exercise, please

My code seems to be working fine, but it still reports the issue:

"Bummer! Hmm, didn't get the expected output. Be sure you're not splitting only on spaces!"

Could you help pls?

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.
import re

def word_count(some_string):
    some_dict = {}
    list_of_words = re.split(' |,|\.', some_string.lower())

    for i in list_of_words:
        try:
            some_dict[i] += 1
        except KeyError:
            some_dict[i] = 1

    print(some_dict)
    return some_dict

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

I got that same message and it took me a while to work out what exactly it was looking for.

I had spent a while wondering why words = string.split(' ') wasn't working and was getting the same issue as you, and do feel like the message isn't really pointing anyone in the right direction.

Since you had the initiative to try a regular expression, I am sure I don't have to explain why words = re.split('\s', string) :)

Evgeny Shilov
Evgeny Shilov
21,283 Points

Worked! Thank you, Umesh!! The correct answer is quite surprising)