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

Michael Davis
Michael Davis
Courses Plus Student 12,508 Points

Python Collections Challenge

For the challenge, my code works in Workspace using the given example, however it fails when being submitted.

The error message I'm getting is:

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

It doesn't give me any information that would allow me to debug my work, so I have nothing to go on here.

Any help is appreciated.

# 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(msg):
    words = msg.lower()
    wordDict = {}

    for s in words:
            wordDict[s] = wordDict[s] + 1
        except KeyError:
            wordDict[s] = 1

    return wordDict 

2 Answers

Michael Davis
Michael Davis
Courses Plus Student 12,508 Points

Solved it, I was missing a variable assignment. One of the mutations to the string I assumed was a destructive mutation (modified the string itself), when it actually made a copy that I needed to capture.

james south
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

it tells you to split on all whitespace. you are only splitting on the space character. other whitespace characters include newlines and tabs among others. split on () instead of (" ").

Michael Davis
Michael Davis
Courses Plus Student 12,508 Points

I made the change from .split(" ") to .split() and received the same error.