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

Task error

Hi I keep getting a task error but I know the output is correct because it matches the examples.

In addition to the attached snippet, I also tried doing it without the wordsunique variable with this block of code and it also gives the same output as the example

def word_count(x):
    try:
        words = x.lower().split(" ")
        output = {}
        for word1 in words:
            count = 0
            for word2 in words:
                if word1 == word2:
                    count = count + 1
                else:
                    continue
            output.update({word1: count})
        return output
    except AttributeError:
        print("You have the wrong type of variable")
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(x):
    try:
        output = {}
        words = x.lower().split(" ")
        wordsunique = list(set(words))
        for word1 in wordsunique:
            count = 0
            for word2 in words:
                if word1 == word2:
                    count = count + 1
                else:
                    continue
            output.update({word1: count})
        return output
    except AttributeError:
        print("You have the wrong type of variable")

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

I ran into this one myself the other day. The challenge asks you to split on all whitespace. A single space (" ") is not every possible whitespace character. By default, the split method will operate on all whitespace if you pass it no parameters, which is what you should do here

Thanks Michael, that makes sense. Appreciate the look over