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

Word_count python. Why is this solution wrong?

Why isn't this correct. When I test it in pycharm I can feed it everything and it works.

wordcount.py
def word_count(s):
    s = s.lower()
    s = s.split(" ")
    d = {}
    for one in s:
        d.update({one: 1})

    for one in s:
        counter = 0
        for two in s:
            if one == two:
                counter += 1
        d.update({one: counter})

    return d

3 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

When I test it with the following:

print(word_count('What do   You   Get from  this   Example  Sam  I  am I  am a ham'))

The incorrect output is below:

{'what': 1, 'do': 1, '': 11, 'you': 1, 'get': 1, 'from': 1, 'this': 1, 'example': 1, 'sam': 1, 'i': 2, 'am': 2, 'a': 1, 'ham': 1}

Don't you want it to produce the following:

{'what': 1, 'do': 1, 'you': 1, 'get': 1, 'from': 1, 'this': 1, 'example': 1, 'sam': 1, 'i': 2, 'am': 2, 'a': 1, 'ham': 1}

Alright but even when I fix this with del d[''] it doesn't let me pass.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

OK, is doing a del d[''] really the way to fix this?

If you fix your split() it will pass.

This worked. Thank you =) The important part was that

split(" ")

only splits one whitespace where as

split()

will split on multiple whitespaces!