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 trialAlexander Davison
65,469 PointsPython Collections: word_count function
I have tested my code in IDLE (the official Python IDE) and my function worked perfectly, however, in the code challenge all it responds is "Bummer! Hmm... Didn't get expected output".
What could be possibly be wrong?
Any help appreciated.
~Alex
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 2, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.
def word_count(x):
words = x.lower().split(" ")
result = {}
for word in words:
if word in result.keys():
result[word] += 1
else:
result[word] = 1
return result
Seth Kroger
56,413 PointsI decided to try this one out and after banging my head on a couple variations it's getting hung up on split(" ")
vs. split()
. The challenge will pass if you use the latter, but not the former.
def word_count(string):
words = string.lower().split()
result = {}
for word in words:
if word not in result:
result[word] = 0
result[word] += 1
return result
Alexander Davison
65,469 PointsCheck my post below Ryan S's answer.
It says "I also just figured out that calling the .split() function without using arguments then it will split by any kind of whitespace".
I hope this helps you :)
Steven Parker
231,248 PointsI got your message.
It looks like you already were effectively assisted.
2 Answers
Ryan S
27,276 PointsHey Alex,
I found that if you remove the space character in your .split()
method, it will pass. It splits on whitespace by default if you don't supply any arguments.
But I don't know why it isn't working in the challenge if you leave it in. I would've guessed that it does the same thing, especially since it is working on your local machine.
Alexander Davison
65,469 PointsThanks so much! It helped.
I also just figured out that calling the .split()
function without using arguments then it will split by any kind of whitespace while calling the .split()
function with a argument of a space will only split by spaces.
Thanks again!
Ryan S
27,276 PointsOh interesting. That is good to know. Thanks!
Kenneth Love
Treehouse Guest TeacherYep, the supplied string might contain new lines or other whitespace. In the words of "The Zen of Python":
In the face of ambiguity, refuse the temptation to guess.
I've updated the error message to be a bit more precise on this, though.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsKenneth Love Steven Parker Jennifer Nordell Ken Alger