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 not being accepted, I cannot find where a bug is. Every input I have given it has worked wonderfully.

I did not error proof the function, but I don't think we have to do that for this exercise.

If you see where I went wrong, or what input breaks the function, please let me know.

def word_count(string):

    string_list = string.lower().split(' ')
    dict = {}

    for item in string_list:
        if item in dict:
            dict[item] += 1
        else:
            dict.update({item: 1})


    return dict

I can't believe that tiny change worked, I've been beating my head against this nearly half the day.

Thank you so much Brandon Kelly! Thanks for the compliment too, you are the first person to say that I coded something well. It felt good.

Brandon Kelly
Brandon Kelly
3,808 Points

Glad to help! This forum is every bit as much a learning tool as the lessons themselves. I've found lots of great answers here, I'm happy to be getting to the level where I can give back. Good luck, and happy coding.

1 Answer

Brandon Kelly
Brandon Kelly
3,808 Points

Hey Benjamin! I tried running your code in Workspaces and I was perplexed for quite awhile, as it does indeed seem to pass. The way to break your code, and the reason it won't pass is by adding a group of consecutive spaces between words. You'll see that it then counts the blank spaces as a string and gives it a key and value in the dictionary. The default setting of the split() method is to leave the parenthesis empty, which will split the string on whitespaces and not count them. This will pass the challenge... Nice coding, by the way, I didn't realize you could add to dictionaries by referencing the key and then using the '+=' operator. Pretty sweet. Good luck.