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

a a
a a
1,101 Points

I get exactly same return as example but failed challenge task

Python 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Counter
>>>
>>> def word_count(arg):
...     arg_1 = arg.lower()
...     arg_2 = arg_1.split(" ")
...     return(dict(Counter(arg_2)))
...
>>> word_count("I do not like it Sam I Am")
{'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}

I didnt know collection.counter and took a day for this task. Finally I get the same result as I expected but "Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!" this message comes up. I need your help.

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.
from collections import Counter

def word_count(arg):
    arg_1 = arg.lower()
    arg_2 = arg_1.split(" ")
    return (dict(Counter(arg_2)))

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are only splitting on space, not tab, newline etc. to include all whitespace just remove the argument to split so that the parens are empty, split(). then it will split on all whitespace and pass.

a a
a a
1,101 Points

Thanks for quick replay. I passed the task. But I still dont understand what makes me fail the task although the result is the same.

adam naceri
adam naceri
7,163 Points

hello james he means your code only works when there is only one space between words but if there was more than that it wouldnt work and if you only use the .split() it just takes off directly the white space since on your code you typed in one space in the .split argument

Thomas Bråten
Thomas Bråten
1,408 Points

So, I am trying this

from collections import Counter

word_to_count = "I do not like it Sam I Am"

def word_count(arg):
    arg_lowered = arg.lower()
    arg_count = arg.split()
    return (dict(Counter(arg_count)))


word_count(word_to_count)

I'm getting the right output, and as I understood this was the way to clear all the whitespace. But still it doesn't pass on the task? What am I missing?