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 trialDerick Ho
3,113 PointsI have another working solution that solves the given problem but it still tells me that it is incorrect!
You may copy the code and see that it works! The task is to take a string and use the contents as the key to a dictionary and the number of of times an item appears as the value. My code does exactly that and yet the problem tells me it is 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(word):
words = []
dictionary = {}
word = word.lower()
words = word.split(' ')
unique_words = []
for item in words:
if item not in unique_words:
count = 1
unique_words.append(item)
print(item)
dictionary[item] = count
elif item in unique_words:
count += 1
dictionary[item] = count
return dictionary
2 Answers
Nader Alharbi
2,253 PointsHello Derick,
Your code is correct, just replace your split.(' ') with split() and you are ready to go.
I allowed my self to write a more efficient code for you, eliminating some unnecessary steps in your code.
def word_count(string):
dictionary = {}
words = string.lower().split()
for word in words:
if word not in dictionary:
dictionary.update({word : 1})
else:
dictionary[word] += 1
return dictionary
<noob />
17,062 Pointswhich error it gives u