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

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.