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

Ari Decter-frain
PLUS
Ari Decter-frain
Courses Plus Student 1,032 Points

Code works in workspace, but doesn't pass challenge.

I'm running the attached code to try to pass this challenge, and it tells me:

"Bummer! Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!"

It seems to me that I'm dealing adequately with letter case and also that I'm splitting correctly... In workspace, I run the same code with print(dict) at the end instead of return, and it produces:

{'am': 2, 'not': 3, 'do': 2, 'i': 1, 'like': 4, 'sam': 3, 'it': 2}

Which seems identical to the example. Please advise!

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):
    #split string into list of words
    string_list = string.lower().split()
    #save length of each word into a list
    length_list = []
    for word in string_list:
        length_list.append(len(word))
    #add each word into a dictionary as the key with the length as the value
    dict = {}
    n = len(string_list)
    i = 0
    while i < n:   
        dict.update({string_list[i]: length_list[i]})
        i += 1
    #return the dictionary
    return dict

you are not looking for the length of the word, but the COUNT of the number of times the word shows up in the string input. :)

1 Answer