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

Sanoj Amarasiri
Sanoj Amarasiri
1,877 Points

Code challenge in dictionaries: wordcount

I tried this code and "Bummer : try again" appears I don't understand why. I ran it an output is correct. I checked online and importing counter doesn't work either. I cannot figure out any other way. Please someone help me to get through this. :(

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(text):
    list_1 = text.lower().split()
    count = 1
    index = 0
    refer = 1
    dict_1 = {}
    dict_2 = {}
    for word in list_1:
        newlist = list_1[refer:]
        if word in newlist:
            count += 1
            dict_1.update({word : count})
        else:
            count = 1
            dict_2.update({word : count})

        refer += 1
        index += 1
    dict_2.update(dict_1)
    return dict_2
Brennan White
Brennan White
11,646 Points

Did you ever get this to work? I ran the code, too, and it seems to be working fine.

1 Answer

Sanoj Amarasiri
Sanoj Amarasiri
1,877 Points

I figure out shorter way. It worked, This is it.

def word_count(text): dict_1 = {} new_list = text.lower().split() for word in new_list: dict_1[word] = new_list.count(word)

return(dict_1)