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

Alright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it.

Not sure of the issue here, any pointers will help

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(val):
    val=val.lower()
    dist={}
    list1=val.split(" ")

    for x in list1:
        if x in dist:
            dist[x] +=1

        else:
            dist[x]=1

    return dist 

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Prashant,

It took me a while to figure out the problem, but the issue seems to be that you are splitting only on a hard-coded space (" "), whereas the challenge is expecting you to split on any whitespace (which could be " ", but could also be new lines and such).

Try removing the hard-coded space from the split statement:

# Use this:
list1=val.split()
# instead of this:
list1=val.split(" ")

That should fix it!

gr8 thanks for your help, it worked !!