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 trialAdam Teale
10,989 PointsTest not passing - seems to work fine in workspaces
Hey Guys from what I can see in workspaces and in the python interpreter this script should pass. Any ideas? Thanks!
# 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(astring):
b = astring.lower().split(" ")
wordsfound = {}
for word in b:
if word in wordsfound.keys():
wordsfound[word] += 1
else:
wordsfound[word] = 1
return wordsfound
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou are very close! Split on WHITESPACE instead of a literal SPACE. This is the default mode when using .split()
without any arguments.
Adam Teale
10,989 PointsG'day Chris & Julian! Thank you both for your help! In the end the solution was as you suggested Chris. Thank you!
Tonye Jack
Full Stack JavaScript Techdegree Student 12,469 PointsThis is another approach.
def word_count(arg):
words = arg.lower().split()
keys = set(words)
ret = dict(zip(keys, [0 for _ in range(len(keys))]))
for key in words:
if ret.get(key) is not None:
ret[key] += 1
return ret
juliansteffen
9,802 Pointsjuliansteffen
9,802 PointsHe Adam,
did you realize if you call your programm it return every time a different order ?
{'do': 1, 'am': 1, 'it': 1, 'i': 2, 'sam': 1, 'like': 1, 'not': 1}
treehouse:~/workspace$ python wordcounting.py
{'i': 2, 'am': 1, 'it': 1, 'sam': 1, 'like': 1, 'do': 1, 'not': 1}
treehouse:~/workspace$ python wordcounting.py
{'it': 1, 'i': 2, 'not': 1, 'sam': 1, 'do': 1, 'like': 1, 'am': 1}
treehouse:~/workspace$ python wordcounting.py
{'i': 2, 'it': 1, 'not': 1, 'sam': 1, 'do': 1, 'am': 1, 'like': 1}
treehouse:~/workspace$ python wordcounting.py
{'sam': 1, 'do': 1, 'i': 2, 'not': 1, 'like': 1, 'it': 1, 'am': 1}
This is might a problem for the tests. Often there run in test is multiply times and expect always the the answer.
But this is just an fast idea.
Greetings Julian