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 have a correct output on my console, so don't understand why I get a Bummer here, any idea?

def word_count(word): word=word.lower() word=word.split(' ') dico=dict() for item in word: dico.update({item:word.count(item)}) return dico

word_count("I do not like it Sam I Am") Out[111]: {'am': 1, 'do': 1, 'i': 2, 'it': 1, 'like': 1, 'not': 1, 'sam': 1}

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(word):
    word=word.lower()
    word=word.split(' ')
    dico=dict()
    for item in word:
        dico.update({item:word.count(item)})
    return dico

2 Answers

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

HI Basile,

Your problem is with this line:

word=word.split(' ')

On the face of it, this seems fine, especially given the example string that the challenge provides. However, there is a subtle clue about the problem in your error message:

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

Notice that the error says "splitting on all whitespace!". This is not quite the same as what you are doing, which is splitting on single spaces. To see the difference, consider the situation where the test string is "hello to the world!" (two spaces between each word) or where the test string is "hello\tworld".

Take a look at the documentation for str.split(), and look closely at the section where the documentation talks about where sep (the argument that you are making ' ') is left as the default (None).

Hopefully this will point you in the right direction.

Cheers

Alex

Hi Alex,

Thanks a lot for your help, I get it now!

Have a nice day Basile