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 trialJordan James
1,929 PointsI can't tell how my answer is incorrect
What's wrong here?
# 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(str) :
str = str.lower().strip()
words = str.split(" ")
word_count = {word: 0 for word in words}
for word in words:
word_count[word] += 1
return word_count
2 Answers
Stuart Wright
41,120 PointsThe problem is with this line:
words = str.split(" ")
" " is one of several different types of whitespace characters. Others exist, such as tabs and newline characters. The challenge wants the function to split on all of these. Thankfully, this is quite simple, as the method's default behaviour when you pass no argument is to split on all whitespace:
words = str.split()
As an aside, using str as a variable name is not good practise, as it is a built-in type in Python, although you do not need to make this change to pass the challenge.
Jordan James
1,929 PointsHey Thanks for that Stuart, it's always something simple! Yeah I would always use descriptive names but the treehouse problems I try to solve quickly haha. Thanks again!