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 trialTonderai Makumborenga
4,763 PointsWord count challenge
I m trying this code in pycharm and its working but can't pass the challenge, help please
# 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(text):
wordcount = {}
counter = 0
word_list = words.split()
for word in word_list:
word.lower()
if word in wordcount:
counter += 1
wordcount.update({word: counter})
else:
counter = 1
wordcount.update({word: counter})
return wordcount
2 Answers
Brandon Jaus
13,681 Points- You are using
words.split()
instead oftext.split()
to assign yourword_list
variable. - Immutable objects can't be changed in place:
word.lower()
needs to beword = word.lower()
. - It looks like your
if/else
block would cause anIndentationError
. This block would need to be in line withword.lower()
in your code snippet. Keep in mind that unlike other languages, Python relies on white space.
Of course, there is a way to shorten this function substantially. That could be done with a dictionary comprehension like this:
def word_count(text):
words = text.lower().split()
return {word: words.count(word) for word in words}
james south
Front End Web Development Techdegree Graduate 33,271 Pointsi couldn't get your code to work as posted. your parameter is called text, but the body has no operations on text. but if we replace words with text, the code still returns capitalized words. to fix, when you make your list of split words, you can additionally chain the lower method to lowercase them all, as in word_list = text.lower().split().
Tonderai Makumborenga
4,763 PointsThank you
Tonderai Makumborenga
4,763 PointsTonderai Makumborenga
4,763 PointsThank you so much ,it worked
Brandon Jaus
13,681 PointsBrandon Jaus
13,681 PointsGlad I could help!