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 trialRobust Tran
2,802 PointsWord count function problem
I have checked my code in the terminal, and it worked as expected {'do': 1, 'not': 1, 'like': 1, 'sam': 1, 'i': 2, 'am': 1, 'it': 1} on word_count("I do not like it Sam I Am"). But I'm still getting the "Bummer: didn't get the .... etc".
Can someone help me out? Point out where my code is wrong? Thank you
def word_count(sentence):
result = {}
for word in sentence.split(' '):
word = word.lower()
if word not in result:
result[word] = 1
else:
result[word] += 1
return dict(result)
1 Answer
Mike Wagner
23,559 PointsI'm not entirely sure what the root of the issue is, but to solve it you can remove the ' '
from your split() operation. Something about the existence there is confusing the challenge environment. My guess is that it's looking for a double whitespace character, perhaps. Either way, since the split() operation actually accounts for whitespace automatically, there's no need to explicitly define it. Also, even though you'll pass the challenge with it in, there's no need to return dict(result)
because you've already defined result as a dictionary when you created it.
Robust Tran
2,802 PointsRobust Tran
2,802 PointsWow, remove the ' ' from your split() operation <<< Worked. How weird it is!? Thank you :)
Mike Wagner
23,559 PointsMike Wagner
23,559 PointsRobust Tran - of course. Glad I could help. Yeah, I honestly still haven't figured out why that one little change throws an unexpected output. I've been reading over documentation since I answered your question and it seems like it should work either way, which is probably why it worked just fine for you in the terminal. The Treehouse challenges are sometimes quite picky about how they are solved, though, so it could have just checked to make sure you split in a certain way and didn't like that you passed the argument of the whitespace character.