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 trialHarris Handoko
3,932 Pointsword_count challenge task not working correctly: it works on workspace, but it didn't pass the checker
Adding the following to call the function:
string = "I do not like it Sam I Am"
print(word_count(string))
I got:
['i', 'do', 'not', 'like', 'it', 'sam', 'i', 'am']
{'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}
Please tell me where I went wrong?
# 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(string):
word_list = (string.lower()).split()
print(word_list)
word_count = {}
for word in word_list:
count = 1
try:
if word_count[word] >= 1:
word_count[word] = count + 1
except KeyError:
word_count[word] = count
return word_count
1 Answer
Renato Guzman
51,437 PointsTry testing with: "I like people, I like pets and I like ice cream".
Notice that count is always 1 in every iteration. Also I recommend to change the name of your variable word_count
since it is the same as your method.
Hope it helps!
Harris Handoko
3,932 PointsHarris Handoko
3,932 PointsThanks! count = 1 was not correct, how could I forget the age-old += 1 ?!!! ;)