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

I'm getting an Error despite getting the correct answer in the Python Shell. I properly lowercased and split the string.

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):
    dictionary = {}
    string = string.lower()
    string_list = string.split(' ')
    string_set = set(string_list)
    for word in string_set:
        dictionary[word] = string_list.count(word)

    return dictionary

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

You are very close. In the test data for this challenge, the words may be separated by tabs or multiple spaces. Therefore it is better to split on WHITESPACE then splitting on a literal SPACE. To split on WHITESPACE, use the split() method with no argument.

Nathan Rolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Rolf
Python Web Development Techdegree Student 6,118 Points

If we could see the test cases beforehand, we can account for them in our testing. With the vague errors given by the code interface, it is very frustrating to try and debug. This is a major pain.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

I agree. It can be very frustrating when the test case isn't obvious from the description!

While painful, it is these lessons that you get the most growth from. The difference between whitespace and a literal space is now yours forever!

I'm glad you posted on the community forum when getting stuck on a challenge. That's why we're here. Everyone, including me, relies on forums and StackOverfow to track down help as needed.

Best of luck on your continuing Treehouse studies! Post back if you need more help. We'll be here!!

Nathan Rolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Rolf
Python Web Development Techdegree Student 6,118 Points

Not having any debugging information besides the "Bummer!" message is not teaching good debugging techniques. Also, not providing the right answer and/or explanations for quiz answers is very frustrating. That isn't growth.