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 trialShrinivas Ganesan
Courses Plus Student 5,923 PointsCode working fine on workspace but not in the code challenge
I got the following output on the workspace for my code:
Your dict is:
{'sea': 2, 'sells': 1, 'she': 1, 'shore': 1, 'the': 1, 'shells': 1, 'on': 1}
But the same code on the code challenge gives me an output which says ,"Didn't get the count right on some words".
Can't figure out where I'm going wrong with this. I'd really appreciate some help. Thanks!
def word_count(string):
my_string = string.lower().split()
my_string = list(my_string)
my_dict = {}
for i in my_string:
count = 1
if i in my_dict:
count += 1
else:
count = 1
my_dict[i] = count
return my_dict
print("\n Your dict is:")
print(word_count("She sells sea shells on the sea shore"))
2 Answers
Mikael Enarsson
7,056 Pointsdef word_count(string):
my_string = string.lower().split()
my_string = list(my_string)
my_dict = {}
for i in my_string:
count = 1 #This sets the initial value of count to 1 for every item in your list
if i in my_dict:
count += 1 #If item in my_dict, then count += 1, or, in other words 2, following the first point
else:
count = 1 #If it isn't, count = 1, still
my_dict[i] = count #Add 1 or 2 to the key's value (not sure if that's the right term, but you get it), but not, for example, 3
return my_dict
print("\n Your dict is:")
print(word_count("She sells sea shells on the sea shore"))
This should outline the problem, but if it feels unhelpful, comment and I'll try to give some more details.
By the way, you can test for this particular problem if you want, just add a few more "sea"s or something ^^ My result for "She sells sea shells on the sea shore sea sea" was:
Your dict is:
{'the': 1, 'she': 1, 'shells': 1, 'on': 1, 'sea': 2, 'shore': 1, 'sells': 1}
Kenneth Love
Treehouse Guest TeacherSince .split()
always gives back a list, I'm not sure why you're both making it a list
again on the next line.
What if a word appears more than twice in the string? The highest number your code can set it to is 2. You should get the value (the count) for the word that's already in the dictionary, increment that by 1, and then set it again to the new value.
Shrinivas Ganesan
Courses Plus Student 5,923 PointsYeah, I didn't realise .split() returned a list. Thanks for the pointer!
Shrinivas Ganesan
Courses Plus Student 5,923 PointsShrinivas Ganesan
Courses Plus Student 5,923 PointsThank you, this was very helpful. I corrected the code and got it right this time.
Mikael Enarsson
7,056 PointsMikael Enarsson
7,056 PointsNo problem ^^ If you think it deserves it, I would be very grateful if you could mark the answer as "Best answer" (for the points ~.^)