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

Code that seems to be fine not passing.

So when i run the code on my local machine with i get the return value of: {'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}

which seems to be pretty darn close to what the comment says i should be getting. {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}

yet the task fails.

wordcount.py
def word_count(string):
    out = {}
    x = string.lower()
    x = x.split(' ')
    for item in x:
        if item not in out:
            out[item] = 1
        else:
            out[item] += 1
    return out

1 Answer

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

Hi Rob,

The problem is with how you're splitting the string. It's not clear from the challenge description (although it is hinted by the error message) but some of the sample strings that Treehouse will pass into your function have whitespace that isn't just single spaces (maybe double spaces, maybe tabs).

When your function gets passed a string with a double space, e.g., "hello world", your split will create ['hello', '', 'world']. When it gets passed a string with a tab, e.g., "hello\tworld", your split will create ['hello\tworld"].

To split on all whitespace, rather than just single spaces, you use the split method with no arguments.

Cheers

Alex

Thanks, that worked. I feel a bit silly that i didn't try that.