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 trialChristian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 Pointsword count
I've put this in workspaces a few times and got the results I wanted but it's still saying it's not passing. Any ideas?
# 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(value):
dictionary = {}
new_list = []
value = value.lower()
my_list = value.split(" ")
print(my_list)
for word in my_list:
if word == word in new_list:
number += 1
dictionary[word] = number
else:
number = 1
new_list.append(word)
dictionary[word] = number
continue
print(dictionary)
return dictionary
word_count("I I I I do Do do Not not nOt")
word_count("I do not like it Sam I am")
2 Answers
mhjp
20,372 PointsThis challenge really is a pain in the but, the feedback isn't at all useful.
value.split(" ")
should be
value.split()
using split(" ") will only split on a single whitespace using split() will split on all whitespace
Also in your if block you are setting number += 1 this is casing all kinds of weirdness as the value is not reset in each loop, a better way to handle this would be just to set dictionary[word] += 1
Here is how I would do this.
def word_count(value):
dictionary = {}
for word in value.lower().split():
if word in dictionary.keys():
dictionary[word] += 1
else:
dictionary[word] = 1
return dictionary
Cheers!
Paul Heneghan
14,380 PointsI'm wondering if it's because you called the function twice?
Christian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 PointsChristian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 PointsAwesome dude thank you.