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 triala a
1,101 PointsI 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.
# 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
Front End Web Development Techdegree Graduate 33,271 Pointsyou 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.
Thomas Bråten
1,408 PointsSo, 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?
a a
1,101 Pointsa a
1,101 PointsThanks 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
7,163 Pointsadam naceri
7,163 Pointshello 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