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 trialTim Buck
5,919 PointsWord Count dictionary: I thought my code is correct, but keep getting bummer message.
Here is what I have:
def word_count(string):
words_dict = {}
string = string.lower()
words = string.split(" ")
for word in words:
number_of_times = words.count(word)
words_dict[word] = number_of_times
return words_dict
Can someone help me?
# 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):
words_dict = {}
string = string.lower()
words = string.split(" ")
for word in words:
number_of_times = words.count(word)
words_dict[word] = number_of_times
return words_dict
2 Answers
Alexander Davison
65,469 PointsCalling .split(" ")
only splits on spaces. The challenge expects you to split on all whitespace. split
by default splits on all whitespace if you don't pass it any arguments, therefore you needn't (and shouldn't) pass it any arguments.
words = string.split(" ") # Works, but not what the challenge wants
words = string.split() # Pass! :)
Philip Schultz
11,437 PointsHey, just remove the quotations in the split method......then it works