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

Code is working correctly, still getting an error that it is not complete. I tested in workspaces as well and it works.

I am lowercasing the string, splitting on white space, and setting the correct values for each word. The dictionary outputs correctly in workspaces, but I keep getting the error that the code is not correct.

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(str):
    dict = {}
    str = str.lower()
    words = str.split(' ')

    for word in words:
        dict[word] = 0

    for word in words:
        dict[word] += 1

    return dict

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Try running where you put 3 extra spaces between do and Not (sometimes extra spaces are removed from these posts) - or just add extra spaces between any words in your test sentence and see your results:

print(word_count("I do     Not like it Sam I am"))
Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Kirsten,

You've pretty much got it, except for one small oversight. When you check your code, the error comes back and partly says:

Be sure you're lowercasing the string and splitting on all whitespace!

Your code is splitting on spaces only. While spaces are whitespace, there are also many other characters that are read as whitespace, such as newline returns and tabs. So, you're really not splitting on all whitespace.

Note: This isn't made very clear in the instructions, only in the error message (Tagging Craig Dennis for maybe a fix).

Anyway, remember to split on all whitespace, you just pass no arguments into the split() method. So, just change the split(' ') to split(). The rest is perfect.

Nice work!! :) :dizzy: