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

itamar dahan
itamar dahan
1,297 Points

Word Count challenge won't pass even though my code works

Hey

Using my testing in my local environment, i see that i get the expected results. even when using the example in the comments, i get the answer as the example.

But i still get "Bummer!" when i press check =( Any ideas?

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 (s):
    s = s.lower()
    lst = s.split(' ')
    diction = {}
    for item in lst:
        diction[item] = 0

    for key in diction.keys():
        for item in lst:
            if item == key:
                diction[key] += 1
    print(diction)

3 Answers

Bapi Roy
Bapi Roy
14,237 Points

In place of print the result. You need to return the result.

What Bapi Roy said, and uses .split () without any argument, this way it removes all spaces ('TAB' ...)

I dunno how sensitive the language you are using is to spaces, but I see a space between word_count and (s), and maybe you need to get rid of that space.