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

my code is working right in idle but here it is telling wrong output

i used count methods to count no of keys

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(sentence):
    word_lower=sentence.lower()
    word_list=word_lower.split(' ')
    my_dict={}
    for word in word_list:
        my_dict[word]=word_list.count(word)
    return my_dict

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi there,

You've got everything correct, except for one small thing. Yes, this code would return the correct output with this example, but what if someone used tab or a hard-return in the input?

Right now, you're splitting on only spaces by using (' '). You should be splitting on all whitespace. This is done by passing no parameters into the split() method.

Other than that... nice work!! :) :dizzy:

thnks jason i never thought of this !! jason one more question did you complete your treehouse deg and got a moderator job?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hi PANKAJ SHUKLA

I'm glad I was able to help.

Treehouse Moderators are all volunteers and just students like yourself. We aren't actually employed by Team Treehouse. We just help out in the Community with questions and also help to maintain a productive and friendly learning environment.

I hope the rest of your Treehouse journey is enjoyable and educational! :) :dizzy: