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 trialRyan Hallett
5,796 Pointsword_count code challenge
I run this in workspaces and it gives me exactly what I'm looking for. Why is this not passing?
# 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(st):
my_dict = {}
words = st.lower().split(" ")
for word in words:
my_dict.update({word: words.count(word)})
return my_dict
2 Answers
Steven Parker
231,248 PointsWhen I tried your code I got this hint message: "Bummer! Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!"
To split on all whitespace, the argument to split should be empty. Supplying an argument of a space causes it to split only on spaces.
Dawit Tsegaye
950 Pointsdef word_count(string): word_count = {} string = string.lower().split()
for word in string:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
retrun word_count
Ryan Hallett
5,796 PointsRyan Hallett
5,796 PointsAh awesome, thanks. I thought you needed to specify what to split on, ie " " being a single space, but I guess that doesn't cover two spaces etc etc. Nice!