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


Why doesn't it work? Please help. I checked the code on a different editor and it worked just fine.

# 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.

import copy

def word_count(some_string):
    some_string = some_string.lower()
    some_string = some_string.split(" ")
    some_dictionary = {}
    broken = copy.copy(some_string)

    for word in some_string:
        if word in broken:
            some_dictionary[word] = 0

            while True:
                if word in broken:
                    some_dictionary[word] += 1


    return some_dictionary

1 Answer

I'm not sure what you're doing with the copy module and broken, but you should be able to just check if the word is already in the dictionary, increase the count of that word if it is, and otherwise set to 1 if not (which adds it to the dictionary)…

Also, you might get an error saying that you need to split the string on all kinds of whitespace, not just a single space character. This is the default for the split method, so you shouldn't need to pass in an argument to that function.

Good luck!