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

Hung Chuan Yu
Hung Chuan Yu
7,491 Points

The output looks fine but I still can't pass the challenge...

Here is my output (test in IDE):

word_count("I do not like it Sam I Am") {'it': 1, 'like': 1, 'sam': 1, 'not': 1, 'i': 2, 'do': 1, 'am': 1}

looks like as challenge ask for but still showing error. anyone could help?? thanks....

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(get_string):
    word_count_dict = {}
    get_string = get_string.lower()
    words = get_string.split(' ')
    for i in words:
         word_count_dict[i] = 0
    for i in words:
        if i in word_count_dict.keys():
        word_count_dict[i]+=1
    return word_count_dict

word_count("I do not like it Sam I Am") 

2 Answers

Steven Parker
Steven Parker
231,248 Points

:warning: Be careful about testing a challenge in an external REPL or IDE.

If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

I see two (of three) issues:

  • don't split on just spaces — leave the argument empty to split on all white space
  • statements controlled by an if must be indented more than the if itself
  • but you don't really need the if since you preset all words in the first loop

Also, you only need to define the function, not call it.

Yup. I was having the same issue as you until I replaced the split argument to look like this

words = get_string.split()