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

Christopher Porter
Christopher Porter
2,148 Points

My function seems correct in work space but it won't pass me on the challenge.

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

I have run my function in the workspace with multiple different sentences, which all return expected results.

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(sentence):
    sentence = sentence.lower()
    split_sentence = sentence.split(" ")
    word_count_dict = {}
    for word in split_sentence:
        word_count_dict[word] = split_sentence.count(word)
    return word_count_dict

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Christopher,

Nice work trying to debug your code using different sentences. However, it looks like all the sentences you tried just had single spaces. Let's look again at the error message you're getting, and the very subtle clue that it's giving you about where you're going wrong:

Be sure you're ... splitting on all whitespace!

In order to properly test your code, you'll need to try some sentences that have different configurations of whitespace than the example provided. Consider testing with strings like:

"hello  world"

(two consecutive spaces)

"hello\tworld"

(tab character (considered whitespace))

"hello\nworld"

(newline character (considered whitespace))

Then check out the docs for str.split() and see if there's an alternative argument for sep that might give you the result you want.

Cheers

Alex