Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Word Count

Prashant Nayak
Prashant Nayak
2,299 Points

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,410 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!

Prashant Nayak
Prashant Nayak
2,299 Points

gr8 thanks for your help, it worked !!