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

Ravi Kiran
Ravi Kiran
2,196 Points

Solution is working in console. However its not getting accepted

Create a dictionary with word count

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(string):
    pointer = 0
    count_dict = {}
    string_list = string.lower().split(" ")
    for pointer in range(len(string_list)):
        if string_list[pointer] in count_dict:
            count_dict[string_list[pointer]] += 1
        else:
            count_dict[string_list[pointer]] = 1
    return count_dict

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Ravi Kiran ! You're doing terrific and you are so close here! The problem isn't as much with the code as it is with your test data. The challenge asks you to split on all whitepsace. This includes things like tabs and newline characters. I would bet that you haven't tried any data that contains tabs and newline characters. Currently you are splitting only on spaces.

This splits on spaces:

.split(" ")

But this splits on all whitespace:

.split()

Simply removing the quotation marks from within the split() method causes this to pass. Good job!

Hope this helps! :sparkles:

Ravi Kiran
Ravi Kiran
2,196 Points

Thanks, I learnt a lot with just this problem and also from your comment.