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 trialLuca Tardito
Front End Web Development Techdegree Graduate 17,602 Pointshy guys, I've tried a lot of time to understand where is the problem in my code but I didn't find it....
when I tried to check the work the system said "different output"
# 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):
my_list = string.lower().split(" ")
dictionary = {}
for word in my_list:
if word in dictionary:
dictionary[word] += 1
else:
dictionary.update({word:1})
return dictionary
2 Answers
andren
28,558 PointsThe issue is that the challenge wants to you to split on all whitespace. While a space is an example of whitespace it is not the only example: tabs, line breaks, multiple spaces and other things like that are also classified as whitespace.
Conveniently enough the split
method will actually split on all whitespace by default if you don't pass it any arguments, so if you remove your argument to it like this:
def word_count(string):
my_list = string.lower().split()
dictionary = {}
for word in my_list:
if word in dictionary:
dictionary[word] += 1
else:
dictionary.update({word:1})
return dictionary
Then your code will work.
Ignazio Calo
Courses Plus Student 1,819 PointsI tried to find the error but no luck.
def word_count(string):
my_list = string.lower().split(" ")
dictionary = {}
for word in my_list:
if word in dictionary:
dictionary[word] += 1
else:
dictionary.update({word:1})
return dictionary
response = word_count("I do not like it Sam I Am")
from_example = {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
print(from_example == response)
As you can see here your code generate an input equal to the example :(
Luca Tardito
Front End Web Development Techdegree Graduate 17,602 Pointsyeah, I've tried a lot of time with different input and it always works, I don't understand where is the problem, anyway, thanks for your test!
Luca Tardito
Front End Web Development Techdegree Graduate 17,602 PointsLuca Tardito
Front End Web Development Techdegree Graduate 17,602 Pointsthanks so much, you are right!!! good coding!