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 trial

Python Python Collections (2016, retired 2019) Dictionaries Word Count

Word count challenge

I m trying this code in pycharm and its working but can't pass the challenge, help please

wordcount.py
# 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
Brandon Jaus
13,681 Points
  1. You are using words.split() instead of text.split() to assign your word_list variable.
  2. Immutable objects can't be changed in place: word.lower() needs to be word = word.lower().
  3. It looks like your if/else block would cause an IndentationError. This block would need to be in line with word.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}

Thank you so much ,it worked

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

i 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().

Thank you